Use ant package to implement the class of zip file decompression

Source: Internet
Author: User
Tags mkdir

You need a zip implementation when you do something before, the first thing to know is that the ant package's Zip package implements support for the Chinese pathname, and feels that the other should be the same as the zip util in the package, but it's a bit awkward to write, after all, it doesn't provide the Zipinputstream class, Therefore, we have to use getetries to get the entity set of enumerated type, its disadvantage is that it destroys the cascade relationship between entities, so it is not easy to deal with the Util.zip package.

Code a bit of a patchwork of ingredients, it is a paste it, hehe!!!



Import java.io. * ;
Import java.util. * ;
Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipFile;

Public   class Unzip ... {
//zipfilename for the zip file that needs to be unzipped, extplace for the storage path of the extracted file, both must already exist
   Public Static voidextzipfilelist (String zipfilename, string extplace)throws
Exception...{
ZipFile ZipFile= NewZipFile (zipfilename);
Enumeration e=Zipfile.getentries ();
ZipEntry ZipEntry= NULL;
while(E.hasmoreelements ())...{
ZipEntry=(ZipEntry) e.nextelement ();
String EntryName=Zipentry.getname ();
String names[]=Entryname.split ("/");
intlength=names.length;
String Path=Extplace;
for (intv= 0; v<length; v++) ...{
if(v<length- 1) ...{
Path + + names[v] + "/";
New File (path). mkdir ();
}
Else ...{ //last one
          if(Entryname.endswith ("/")) ... { // for directory, create folder
New File (extplace + entryname). mkdir ();
}
Else ...{
InputStream in=Zipfile.getinputstream (ZipEntry);
OutputStream OS= NewFileOutputStream (NewFile (Extplace+
EntryName));
byte[] buf= New byte[1024];
intLen;
while((Len=In.read (BUF))> 0) ...{
Os.write (buf, 0, Len);
}
In.close ();
Os.close ();
}
}
}
}
Zipfile.close ();
}
}


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.