📄 reading zip file.mht
字号:
href=3D"http://www.newsgator.com/ngs/subscriber/subext.aspx?url=3Dhttp://=
feeds.feedburner.com/Newlc"><IMG=20
alt=3D"Subscribe to our RSS feed"=20
src=3D"http://common.newlc.com/images/ngsub1.gif"></A><BR><A=20
href=3D"http://www.newlc.com/en/newlc/sitemap">More feeds...</A>=20
</DIV></DIV></DIV></TD>
<TD class=3Dmain-content id=3Dcontent-both><!-- <div =
class=3D"breadcrumb"><a href=3D"/">Home</a> :: <a =
href=3D"/en/-Tutorials-.html">Tutorials</a> :: <a =
href=3D"/en/-Basics-.html">Basics</a></div> -->
<DIV class=3D"publicite snap_nopreview">
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" border=3D0>
<TBODY>
<TR>
<TD align=3Dmiddle>
<SCRIPT src=3D"" type=3Dtext/javascript></SCRIPT>
<SCRIPT type=3Dtext/javascript>=0A=
<!--=0A=
if (!document.phpAds_used) document.phpAds_used =
=3D ',';=0A=
phpAds_random =3D new String (Math.random()); =
phpAds_random =3D phpAds_random.substring(2,11);=0A=
=0A=
document.write ("<" + "script =
type=3D'text/javascript' src=3D'");=0A=
document.write =
("http://regie.newlc.com/adserver/adjs.php?n=3D" + phpAds_random);=0A=
document.write =
("&what=3Dzone:3&blockcampaign=3D1");=0A=
document.write ("&exclude=3D" + =
document.phpAds_used);=0A=
if (document.referrer)=0A=
document.write ("&referer=3D" + =
escape(document.referrer));=0A=
document.write ("'><" + "/script>");=0A=
//-->=0A=
=0A=
</SCRIPT>
<NOSCRIPT>
<P><A =
href=3D"http://regie.newlc.com/adserver/adclick.php?n=3Da7d09176"=20
target=3D_blank><IMG alt=3D"" src=3D"">=20
</A></P></NOSCRIPT></TD></TR></TBODY></TABLE></DIV>
<H2 class=3Dcontent-title>Reading ZIP File in Symbian OS<BR></H2>
<DIV class=3Dbreadcrumb><A href=3D"http://www.newlc.com/">Home</A> =
:: <A=20
href=3D"http://www.newlc.com/en/-Tutorials-.html">Tutorials</A> :: =
<A=20
=
href=3D"http://www.newlc.com/en/-Basics-.html">Basics</A></DIV><!-- =
start main content -->
<DIV class=3D"node ">
<DIV class=3Dcontent>
<DIV class=3D"taxonomy newlc-terms">in=20
<UL class=3D"links inline">
<LI class=3D"first last main_term_link-1"><A =
class=3Dmain_term_link-1=20
title=3D"This section contains several tutorials that introduce =
some basic things you should know about Symbian OS if you want to =
develop C++ applications for this platform (no matter it is a Series 60 =
or a UIQ app). If you are new to Symbian, you should browse this section =
first !"=20
href=3D"http://www.newlc.com/en/-Basics-.html" =
rel=3Dtag>Basics</A>=20
</LI></UL></DIV><!-- google_ad_section_start -->
<DIV class=3Dspip-content>
<P class=3Dspip>The most popular archive file format used these =
days is ZIP,=20
originally created by Phil Katz from the modification of ARC =
format.=20
Symbian OS provides a class, called CZipFile, to read ZIP file. =
This class=20
is supported by Symbian since version 7.x. This article will show =
how to=20
use CZipFile and give an example. You can compile and run the =
example in=20
Series 60 2.x or UIQ 2.x.</P>
<P class=3Dspip>Although CZipFile is documented in Symbian SDKs, =
the=20
explanation is far from enough. Another challenge, there is no =
example how=20
to use the class. That's why I write this article to share my =
experience=20
reading ZIP file in Symbian OS. I hope this would be useful for =
you.</P>
<H3 class=3Dspip>Reading ZIP File</H3>
<P class=3Dspip>Let's start with a simple case, open a ZIP file =
and display=20
information of all the files inside the ZIP file. Firstly, we need =
to=20
create a new instance of CZipFile. There is a two-phase =
constructor that=20
you can use to create it.</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr align=3Dleft>CZipFile* zipFile =
=3D=20
CZipFile::NewL(fileSession,=20
aCompressedFile);<BR>CleanupStack::PushL(zipFile);</DIV></TT>
<P></P>
<P class=3Dspip>The first parameter of the constructor is a =
session of File=20
Server. The second parameter is the file name of the ZIP file.</P>
<P class=3Dspip>Next, we have to get the iterator for the ZIP file =
to=20
iterate all the files one by one. It can be done by calling=20
CZipFile::GetMembersL(). The return value is an instance of=20
CZipFileMemberIterator class.</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr =
align=3Dleft>CZipFileMemberIterator* members =3D=20
=
zipFile->GetMembersL();<BR>CleanupStack::PushL(members);</DIV></TT>
<P></P>
<P class=3Dspip>Note that the ownership of members will be passed =
to the=20
caller, thus we have to delete it after we are done.</P>
<P class=3Dspip>Now, how can we get the instance of each file? By =
calling=20
CZipFileMemberIterator::NextL(). We have to call this method until =
it=20
returns 0, which means there is no more file. Look at the =
following=20
example:</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr align=3Dleft>CZipFileMember* =
member;<BR>while=20
((member =3D members->NextL()) !=3D 0)<BR> =
=20
{<BR> console->Printf(<BR> =
=20
KInfoMessage,<BR> =
=20
=
member->Name(),<BR> =20
=20
member->CompressedSize(), member->UncompressedSize()); =
=20
<BR> delete =
member;<BR> =20
}</DIV></TT>
<P></P>
<P class=3Dspip>The example above prints the name, compressed size =
and=20
uncompressed size of all the files in the ZIP file. Note that we =
have to=20
delete member because the ownership is passed to caller.</P>
<P class=3Dspip>Finally, don't forget to release all the resources =
that we=20
have allocated.</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr =
align=3Dleft>CleanupStack::PopAndDestroy(); //=20
members<BR>CleanupStack::PopAndDestroy(); // zipFile</DIV></TT>
<P></P>
<P class=3Dspip>If we have a ZIP file that contains three file, =
i.e.=20
Example.txt, Example.dat and Example.png, the output will look =
like this=20
(you can download this ZIP file at the end of this article).</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr align=3Dleft>Example.txt - 11 -=20
11<BR>Example.dat - 15 - 180<BR>Example.png - 4393 - =
4393</DIV></TT>
<P></P>
<H3 class=3Dspip>Extracting a File from ZIP File</H3>
<P class=3Dspip>This section shows how to extract a specific file =
from a ZIP=20
file. As the previous example, the first step is to create the =
instance of=20
CZipFile.</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr align=3Dleft>CZipFile* zipFile =
=3D=20
CZipFile::NewL(fileSession,=20
aCompressedFile);<BR>CleanupStack::PushL(zipFile);</DIV></TT>
<P></P>
<P class=3Dspip>Here, we don't need an iterator because we are =
interested=20
only in a specific file. The method that we have to call to get =
the=20
instance of it is CZipFile::CaseInsensitiveMemberL(). This method =
requires=20
a parameter, that is the file name that you want to access.</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr align=3Dleft>CZipFileMember* =
member =3D=20
=
zipFile->CaseInsensitiveMemberL(aFileName);<BR>CleanupStack::PushL(mem=
ber);</DIV></TT>
<P></P>
<P class=3Dspip>Once again, we have to delete member after we have =
finished=20
using it. The next step is to the input stream and use the Read() =
method=20
to extract the file. The input stream of a file inside ZIP file is =
RZipFileMemberReaderStream. The method used to get the input =
stream is=20
CZipFile::GetInputStreamL().</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr =
align=3Dleft>RZipFileMemberReaderStream*=20
stream;<BR>zipFile->GetInputStreamL(member,=20
stream);<BR>CleanupStack::PushL(stream);</DIV></TT>
<P></P>
<P class=3Dspip>The following code shows how to read the file. =
Before=20
reading the file, the code allocates a buffer to store with the =
size of=20
member->UncompressesedSize().</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr align=3Dleft>HBufC8* buffer =3D=20
HBufC8::NewLC(member->UncompressedSize());<BR>TPtr8=20
=
bufferPtr(buffer->Des());<BR>User::LeaveIfError(stream->Read(buffer=
Ptr,=20
member->UncompressedSize()));</DIV></TT>
<P></P>
<P class=3Dspip>If your file is quite huge, do not use "one-shot" =
Read()=20
like the example above. It is not good because it will block your =
program.=20
Instead, read using a small block of buffer and do it inside an =
active=20
object. This example use "one-shot" Read() for simplicity =
reason.</P>
<P class=3Dspip>Now we have the extracted file in a buffer. You =
can use it=20
right away, or alternatively you can write it to a file. Here is =
the=20
example how to write it to a file.</P>
<P class=3Dspip><TT></P>
<DIV class=3Dspip_code dir=3Dltr align=3Dleft>RFile=20
file;<BR>User::LeaveIfError(file.Replace(fileSession, fileName,=20
=
EFileWrite));<BR>CleanupClosePushL(file);<BR>User::LeaveIfError(file.Writ=
e(*buffer));</DIV></TT>
<P></P>
<P class=3Dspip>Finally, do not forget to release all the =
allocated=20
resources.</P>
<P class=3Dspip><TT><SPAN class=3Dspip_code=20
dir=3Dltr>CleanupStack::PopAndDestroy(5); // file, buffer, stream, =
member,=20
zipFile</SPAN></TT></P>
<H3 class=3Dspip>Example</H3>
<P class=3Dspip>The following file contains the complete source =
code of the=20
two examples above. This example is a console program. The output =
of the=20
compiled example is a standalone .exe file, ZipExample.exe. Copy =
this file=20
to your device at c:\systems\programs or any other directories. =
Before=20
running the example, make sure that you have copied =
\group\Example.zip to=20
c:\data\Example.zip on your device too.</P>
<P class=3Dspip>The example does two things, i.e.: <BR><IMG =
class=3Dspip_puce=20
alt=3D-=20
=
src=3D"http://www.newlc.com/sites/all/themes/zen/images/blue_bullet.gif">=
Display=20
information of all the files contained in c:\data\Example.zip. It =
is done=20
in IteratorExampleL() function. <BR><IMG class=3Dspip_puce alt=3D- =
=
src=3D"http://www.newlc.com/sites/all/themes/zen/images/blue_bullet.gif">=
Extract=20
Example.txt from c:\data\Example.zip and save it to =
c:\data\Example.txt.=20
It is done in ExtractionExampleL() function.</P>
<P class=3Dspip><SPAN class=3Dinline_center><A=20
title=3D"Download: ZipExample.zip (9.07 KB)"=20
href=3D"http://www.newlc.com/files/ZipExample.zip"><IMG =
class=3Dinline=20
title=3DZipExample.zip=20
style=3D"BORDER-RIGHT: #6191c5 1px solid; BORDER-TOP: #6191c5 1px =
solid; BORDER-LEFT: #6191c5 1px solid; WIDTH: 48px; BORDER-BOTTOM: =
#6191c5 1px solid; HEIGHT: 48px"=20
alt=3DZipExample.zip src=3D""></A><BR><A=20
title=3D"Download: ZipExample.zip (9.07 KB)"=20
=
href=3D"http://www.newlc.com/files/ZipExample.zip">ZipExample.zip</A></SP=
AN></P>
<H3 class=3Dspip>Further Reading</H3>
<P class=3Dspip><IMG class=3Dspip_puce alt=3D-=20
=
src=3D"http://www.newlc.com/sites/all/themes/zen/images/blue_bullet.gif">=
<A=20
class=3Dspip_out=20
=
href=3D"http://www.pkware.com/business_and_developers/developer/appnote/"=
>.ZIP=20
File Format Specification</A> - specification from the creator of =
PKZIP,=20
the first program that supports ZIP file. <BR><IMG =
class=3Dspip_puce alt=3D-=20
=
src=3D"http://www.newlc.com/sites/all/themes/zen/images/blue_bullet.gif">=
<A=20
class=3Dspip_out=20
=
href=3D"http://www.symbian.com/developer/techlib/v8.1adocs/doc_source/ref=
erence/reference-cpp/N102C2/CZipFileClass.html">CZipFile</A>=20
- official documentation of class CZipFile from Symbian web=20
site.</P></DIV><!-- google_ad_section_end -->
<DIV class=3D"field field-type-text field-field-ps">
<DIV class=3Dfield-items>
<DIV class=3Dfield-item>
<DIV class=3Dspip-content></DIV></DIV></DIV></DIV></DIV>
<DIV class=3Dinfo><A=20
href=3D"http://www.newlc.com/en/-Tutorials-.html">Tutorial</A> =
posted <ABBR=20
class=3Dcreated title=3D2005-10-15T10:14:01+0100>October 15th, =
2005</ABBR> by=20
<A title=3D"View user profile."=20
=
href=3D"http://www.newlc.com/en/_Antony-Pranata_.html">antonypranata</A> =
<SPAN class=3Dterms>categories [=20
<UL class=3D"links inline">
<LI class=3D"first last main_term_link-1"><A =
class=3Dmain_term_link-1=20
title=3D"This section contains several tutorials that introduce =
some basic things you should know about Symbian OS if you want to =
develop C++ applications for this platform (no matter it is a Series 60 =
or a UIQ app). If you are new to Symbian, you should browse this section =
first !"=20
href=3D"http://www.newlc.com/en/-Basics-.html" =
rel=3Dtag>Basics</A>=20
</LI></UL>]</SPAN> <SPAN class=3Dlinks>
<UL class=3D"links inline">
<LI class=3D"first last comment_forbidden"><SPAN=20
class=3Dcomment_forbidden><A=20
=
href=3D"http://www.newlc.com/en/user/login?destination=3Dnode/705%2523com=
ment_form">Login</A>=20
or <A=20
=
href=3D"http://www.newlc.com/en/user/register?destination=3Dnode/705%2523=
comment_form">register</A>=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -