Java decompress zip-decompress multiple files or folders

Source: Internet
Author: User
Tags decompress zip

Java decompress zip-multiple files (including folders)


Compress multiple files and folders to decompress complicated file directories.

The compression method uses variable parameters. one or more files can be compressed. Arrays can be written or written to the parameter list one by one...

[Java]
ZipFiles (zip, "abc", new File ("d:/English"), new File ("d:/release data .xls "));

Test file directory structure:

 

Compressed Content: English folder and two excel files at the same level

[Java]
File [] files = new File [] {new File ("d:/English"), new File ("d:/release data .xls"), new File ("d: /Chinese name .xls ")};

The following is the compressed code:
[Java]
/**
* Compressed file-Because out is outside recursive calling, a method is encapsulated
* Call ZipFiles (ZipOutputStream out, String path, File... srcFiles)
* @ Param zip
* @ Param path
* @ Param srcFiles
* @ Throws IOException
* @ Author isea533
*/
Public static void ZipFiles (File zip, String path, File... srcFiles) throws IOException {
ZipOutputStream out = new ZipOutputStream (new FileOutputStream (zip ));
ZipTest. ZipFiles (out, path, srcFiles );
Out. close ();
System. out. println ****************** *");
}
/**
* Compressed File-File
* @ Param zipFile zip file
* @ Param srcFiles: Compressed source file
* @ Author isea533
*/
Public static void ZipFiles (ZipOutputStream out, String path, File... srcFiles ){
Path = path. replaceAll ("\\*","/");
If (! Path. endsWith ("/")){
Path + = "/";
}
Byte [] buf = new byte [1, 1024];
Try {
For (int I = 0; I <srcFiles. length; I ++ ){
If (srcFiles [I]. isDirectory ()){
File [] files = srcFiles [I]. listFiles ();
String srcPath = srcFiles [I]. getName ();
SrcPath = srcPath. replaceAll ("\\*","/");
If (! SrcPath. endsWith ("/")){
SrcPath + = "/";
}
Out. putNextEntry (new ZipEntry (path + srcPath ));
ZipFiles (out, path + srcPath, files );
}
Else {
FileInputStream in = new FileInputStream (srcFiles [I]);
System. out. println (path + srcFiles [I]. getName ());
Out. putNextEntry (new ZipEntry (path + srcFiles [I]. getName ()));
Int len;
While (len = in. read (buf)> 0 ){
Out. write (buf, 0, len );
}
Out. closeEntry ();
In. close ();
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}

During compression, you can determine the folder and then recursively compress the file.

 

 

Then decompress the package:

[Java]
/**
* Decompress the package to the specified directory.
* @ Param zipPath
* @ Param descDir
* @ Author isea533
*/
Public static void unZipFiles (String zipPath, String descDir) throws IOException {
UnZipFiles (new File (zipPath), descDir );
}
/**
* Decompress the file to the specified directory.
* @ Param zipFile
* @ Param descDir
* @ Author isea533
*/
@ SuppressWarnings ("rawtypes ")
Public static void unZipFiles (File zipFile, String descDir) throws IOException {
File pathFile = new File (descDir );
If (! PathFile. exists ()){
PathFile. mkdirs ();
}
ZipFile zip = new ZipFile (zipFile );
For (Enumeration entries = zip. getEntries (); entries. hasMoreElements ();){
ZipEntry entry = (ZipEntry) entries. nextElement ();
String zipEntryName = entry. getName ();
InputStream in = zip. getInputStream (entry );
String outPath = (descDir + zipEntryName). replaceAll ("\\*","/");;
// Determine whether the path exists. If the path does not exist, create a file path.
File file = new File (outPath. substring (0, outPath. lastIndexOf ('/')));
If (! File. exists ()){
File. mkdirs ();
}
// Determine whether the full file path is a folder. If the file has been uploaded, you do not need to decompress the package.
If (new File (outPath). isDirectory ()){
Continue;
}
// Output file path information
System. out. println (outPath );

OutputStream out = new FileOutputStream (outPath );
Byte [] buf1 = new byte [1024];
Int len;
While (len = in. read (buf1)> 0 ){
Out. write (buf1, 0, len );
}
In. close ();
Out. close ();
}
System. out. println ***************** ***");
}

During decompression, you can determine whether to create a non-existent folder for the folder, and create only the folder without decompression .. because the decompression is for the file, not the folder, you need to create the folder yourself.

 

Test method:

[Java]
Public static void main (String [] args) throws IOException {
/**
* Compressed file
*/
File [] files = new File [] {new File ("d:/English"), new File ("d:/release data .xls"), new File ("d: /Chinese name .xls ")};
File zip = new File ("d:/compressed .zip ");
ZipFiles (zip, "abc", files );

/**
* Decompress the file
*/
File zipFile = new File ("d:/compressed .zip ");
String path = "d:/zipfile /";
UnZipFiles (zipFile, path );
}

The test method does not handle any exceptions. This is not correct. Do not imitate it.
Output result:

[Java]
Abc/English/templete.xls
Abc/English/Chinese/csdn/isea/533/abc/templete.xls
Abc/English/Chinese/csdn/isea/533/abc/zipfile2/templete.xls
Abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/templete.xls
Abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls
Abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/ .xls
Abc/English/Chinese/csdn/isea/533/abc/zipfile2/getting stuck .xls
Abc/English/Chinese/csdn/isea/templete.xls
Abc/English/Chinese/csdn/isea/leader .xls
Abc/English/Chinese/csdn/templete.xls
Abc/English/LinkedIn .xls
Abc/release data .xls
Abc/Chinese name .xls
*******************
D:/zipfile/abc/Chinese name .xls
D:/zipfile/abc/release data .xls
D:/zipfile/abc/English/leading folder .xls
D:/zipfile/abc/English/Chinese/csdn/templete.xls
D:/zipfile/abc/English/Chinese/csdn/isea/CAPTCHA .xls
D:/zipfile/abc/English/Chinese/csdn/isea/templete.xls
D:/zipfile/abc/English/Chinese/csdn/isea/533/abc/templete.xls
D:/zipfile/abc/English/templete.xls
D:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/templete.xls
D:/zipfile/abc/English//csdn/isea/533/abc/zipfile2/zipfile/abc/templete.xls
D:/zipfile/abc/English//csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls
D:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/card picking .xls
D:/zipfile/abc/English/Chinese/csdn/isea/533/abc/zipfile2/ .xls
********************

 

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.