Java compression and decompression class

Source: Internet
Author: User
Java ziputil

 Import java. Io. bufferedinputstream;
Import java. Io. bufferedoutputstream;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. enumeration;
Import java.util.zip. zipentry;
Import java.util.zip. zipfile;
Import java.util.zip. zipoutputstream;

Public class ziputil {
Static final int buffer = 2048;

Public static void zip (string zipfilename, string sourcepath ){
Try {
Bufferedinputstream origin = NULL;
Fileoutputstream DEST = new fileoutputstream (zipfilename );
Zipoutputstream out = new zipoutputstream (New bufferedoutputstream (
DEST ));
Byte data [] = new byte [buffer];
File F = new file (sourcepath );
File files [] = f. listfiles ();
For (INT I = 0; I <files. length; I ++ ){
Fileinputstream Fi = new fileinputstream (files [I]);
Origin = new bufferedinputstream (FI, buffer );
Zipentry entry = new zipentry (files [I]. getname ());
Out. putnextentry (entry );
Int count;
While (COUNT = origin. Read (data, 0, buffer ))! =-1 ){
Out. Write (data, 0, count );
}
Origin. Close ();
}
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}

Public static void unzip (string zipfilename, string targetpath ){
Try {
Zipfile = new zipfile (zipfilename );
Enumeration EMU = zipfile. Entries ();
Int I = 0;
While (EMU. hasmoreelements ()){
Zipentry entry = (zipentry) EMU. nextelement ();

If (entry. isdirectory ()){
New file (targetpath + entry. getname (). mkdirs ();
Continue;
}
Bufferedinputstream Bis = new bufferedinputstream (zipfile
. Getinputstream (entry ));
File file = new file (targetpath + "/" + entry. getname ());

File parent = file. getparentfile ();
If (parent! = NULL &&(! Parent. exists ())){
Parent. mkdirs ();
}
Fileoutputstream Fos = new fileoutputstream (File );
Bufferedoutputstream Bos = new bufferedoutputstream (FOS, buffer );
Int count;
Byte data [] = new byte [buffer];
While (COUNT = bis. Read (data, 0, buffer ))! =-1 ){
Bos. Write (data, 0, count );
}
Bos. Flush ();
Bos. Close ();
Bis. Close ();
}
Zipfile. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}

/**
* Decompress all ZIP files in a folder at the original directory level.
* @ Param dir original folder
*/
Public static void unzipdir (string DIR ){
Arraylist list = fileutil. getfilelist (Dir, "Zip ");
For (Object object: List ){
String filename = object. tostring ();
System. Out. println (filename );
String unzipdir = fileutil. getfilepathwithoutsuffix (filename );
If (! (New file (unzipdir). isdirectory ())){
New file (unzipdir). mkdir ();
}
Ziputil. Unzip (filename, unzipdir );
}
}

Public static void main (string argv []) {
Unzipdir ("E:/Temp ");
}
}

 

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.