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 ");
}
}