Symbian read/write zip

Source: Internet
Author: User

The most popular archive file format used these days is
Zip, originally created by Phil Katz from the modification of Arc
Format. Symbian OS provides a class, called czipfile, to read ZIP file.
This class is supported by Symbian since version 7. X. This article will
Show how to use czipfile and give an example. You can compile and run
The example in Series 60 2.x or uiq 2. x.

Although czipfile is already ented in Symbian sdks,
Explanation is far from enough. Another challenge, there is no example
How to Use the class. That's why I write this article to share my
Experience reading zip file in Symbian OS. I hope this wocould be useful
For you.

Reading ZIP file

Let's start with a simple case, open a zip file and
Display information of all the files inside the ZIP file. Firstly, we
Need to create a new instance of czipfile. There is a two-phase
Constructor that you can use to create it.

Czipfile * zipfile = czipfile: newl (filesession, acompressedfile );
Cleanupstack: pushl (zipfile );

The first parameter of the constructor is a session of file server. The second parameter is the file name of the ZIP file.

Next, we have to get the iterator for the ZIP file
Iterate all the files one by one. It can be done by calling
Czipfile: getmembersl (). The return value is an instance
Czipfilememberiterator class.

Czipfilememberiterator * Members = zipfile-> getmembersl ();
Cleanupstack: pushl (members );

Note that the ownership of members will be passed to the caller, thus we have to delete it after we are done.

Now, how can we get the instance of each file? By
Calling czipfilememberiterator: nextl (). We have to call this method
Until it returns 0, which means there is no more file. Look at
Following example:

Czipfilemember * member;
While (member = members-> nextl ())! = 0)
{
Console-> printf (
Kinfomessage,
Member-> name (),
Member-> compressedsize (), member-> uncompressedsize ());
Delete member;
}

The example above prints the name, compressed size and
Uncompressed size of all the files in the ZIP file. Note that we have
To delete member because the ownership is passed to caller.

Finally, don't forget to release all the resources that we have allocated.

Cleanupstack: popanddestroy (); // members
Cleanupstack: popanddestroy (); // zipfile

If we have a zip file that contains three file, I. e.
Example.txt, example. dat and example.png, the output will look like
This (you can download this ZIP file at the end of this Article ).

Example.txt-11-11
Example. dat-15-180
Example.png-4393-4393

Extracting a file from ZIP file

This section shows how to extract a specific file from
A zip file. As the previous example, the first step is to create
Instance of czipfile.

Czipfile * zipfile = czipfile: newl (filesession, acompressedfile );
Cleanupstack: pushl (zipfile );

Here, we don't need an iterator because we are
Interested only in a specific file. The method that we have to call
Get the instance of it is czipfile: caseinsensitivememberl (). This
Method requires a parameter, that is the file name that you want
Access.

Czipfilemember * member = zipfile-> caseinsensitivememberl (afilename );
Cleanupstack: pushl (member );

Once again, we have to delete Member after we have
Finished using it.
The next step is to the input stream and use the read () method
Extract the file. The input stream of a file inside ZIP file is
Rzipfilememberreaderstream. The method used to get the input stream is
Czipfile: getinputstreaml ().

Rzipfilememberreaderstream * stream;
Zipfile-> getinputstreaml (Member, stream );
Cleanupstack: pushl (Stream );

The following code shows how to read the file. Before
Reading the file, the Code allocates a buffer to store with the size
Member-> uncompressedsize ().

Hbufc8 * buffer = hbufc8: newlc (member-> uncompressedsize ());
Tptr8 bufferptr (buffer-> des ());
User: leaveiferror (Stream-> Read (bufferptr, member-> uncompressedsize ()));

If your file is quite huge, do not use "one-shot"
Read () Like the example abve. It is not good because it will block
Your program. Instead, read using a small block of buffer and do it
Inside an active object. This example use "one-shot" Read ()
Simplicity reason.

Now we have the extracted file in a buffer. You can use
It right away, or alternatively you can write it to a file. Here is
Example how to write it to a file.

Rfile file;
User: leaveiferror (file. Replace (filesession, filename, efilewrite ));
Cleanupclosepushl (File );
User: leaveiferror (file. Write (* buffer ));

Finally, do not forget to release all the allocated resources.

Cleanupstack: popanddestroy (5); // file, buffer, stream, Member, zipfile

Example

The following file contains the complete source code
The two examples above. This example is a console program. The output
Of the compiled example is a standalone. EXE file, zipexample.exe. Copy
This file to your device at C:/systems/programs or any other
Directories. Before running the example, make sure that you have copied
/Group/example.zip to C:/data/example.zip on your device too.

The example does two things, I. e .:


Display information of all the files contained in C:/data/example.zip. It is done in iteratorexamplel () function.


Extract example.txt from C:/data/example.zip and save it to C:/data/example.txt. It is done in extractionexamplel () function.

 

Address: http://www.newlc.com/en/Reading-ZIP-File-in-Symbian-OS.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.