Android decompression method

Source: Internet
Author: User

/**
* Decompress the compressed file containing folders.
*
* @ Param zipFile
* @ Param folderPath
* @ Throws ZipException
* @ Throws IOException
*/
Public void upZipFile (File zipFile, String folderPath) throws ZipException,
IOException {
File desDir = new File (folderPath );
If (! DesDir. exists ()){
// Create the target directory
DesDir. mkdirs ();
}


ZipFile zf = new ZipFile (zipFile );
For (Enumeration <?> Entries = zf. entries (); entries. hasMoreElements ();){
ZipEntry entry = (ZipEntry) entries. nextElement ());
If (entry. isDirectory ()){
String tmpStr = folderPath + File. separator + entry. getName ();
TmpStr = new String (tmpStr. getBytes ("8859_1"), "GB2312 ");
File folder = new File (tmpStr );
Folder. mkdirs ();
} Else {
InputStream is = zf. getInputStream (entry );
String str = folderPath + File. separator + entry. getName ();
// Convert the encoding to avoid Chinese garbled characters
Str = new String (str. getBytes ("8859_1"), "GB2312 ");
File desFile = new File (str );
If (! DesFile. exists ()){
// Create the target file
DesFile. createNewFile ();
}
OutputStream OS = new FileOutputStream (desFile );
Byte [] buffer = new byte [1024];
Int realLength;
While (realLength = is. read (buffer)> 0 ){
OS. write (buffer, 0, realLength );
OS. flush ();
}
Is. close ();
OS. close ();
}


}
Zf. close ();

}

 

 

 

/**
* Decompress a compressed package without folders
*
* @ Param zipFile
* @ Param folderPath
* @ Throws ZipException
* @ Throws IOException
*/
Public void upZipFile (File zipFile, String folderPath) throws ZipException,
IOException {
File desDir = new File (folderPath );
If (! DesDir. exists ()){
// Create the target directory
DesDir. mkdirs ();
}


ZipFile zf = new ZipFile (zipFile );
For (Enumeration <?> Entries = zf. entries (); entries. hasMoreElements ();){
ZipEntry entry = (ZipEntry) entries. nextElement ());
InputStream is = zf. getInputStream (entry );
String str = folderPath + File. separator + entry. getName ();
// Convert the encoding to avoid Chinese garbled characters
Str = new String (str. getBytes ("8859_1"), "GB2312 ");
File desFile = new File (str );
If (! DesFile. exists ()){
File fileParentDir = desFile. getParentFile ();
If (! FileParentDir. exists ()){
// Create the parent directory of the target file
FileParentDir. mkdirs ();
}
// Create the target file
DesFile. createNewFile ();
}
OutputStream OS = new FileOutputStream (desFile );
Byte [] buffer = new byte [1024];
Int realLength;
While (realLength = is. read (buffer)> 0 ){
OS. write (buffer, 0, realLength );
OS. flush ();
}
Is. close ();
OS. close ();
}
Zf. close ();
}

 

 

 

Related Article

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.