Public class gzip {
Private Static log = logfactory. getlog (config. Class );
Public final static simpledateformat date_format = new simpledateformat ("yyyy-mm-dd-hh-mm-SS ");
/**
* @ Param path: the path to be compressed. It can be a directory or a file.
* @ Param basepath if path is a directory, it is generally new file (PATH). The function is to make the output ZIP file use this directory as the root directory. If it is null, it only compresses the file, does not understand the pressure directory.
* @ Param Zo compresses the output stream
* @ Param isrecursive Recursion
* @ Param isoutblankdir whether to output an empty directory. To make the output empty directory true, the basefile is not null.
* @ Throws ioexception
*/
Public static void main (string [] ARGs ){
Indexertozip ("D: // makeindexs", "d: // makeindexs", "makeindexs_bak ");
}
Public static void zip (string path, file basepath, zipoutputstream Zo, Boolean isrecursive, Boolean isoutblankdir) throws ioexception {
File infile = new file (PATH );
File [] files = new file [0];
If (infile. isdirectory () {// is the Directory
Files = infile. listfiles (New keywordfilefilter (". Lock", false ));
} Else if (infile. isfile () {// Yes
Files = new file [1];
Files [0] = infile;
}
Byte [] Buf = new byte [1, 1024];
Int Len;
// System. Out. println ("basefile:" + basefile. getpath ());
For (INT I = 0; I <files. length; I ++ ){
String pathname = "";
If (basepath! = NULL ){
If (basepath. isdirectory ()){
Pathname = files [I]. getpath (). substring (basepath. getpath (). Length () + 1 );
} Else {// File
Pathname = files [I]. getpath (). substring (basepath. getparent (). Length () + 1 );
}
} Else {
Pathname = files [I]. getname ();
}
System. Out. println (pathname );
If (files [I]. isdirectory ()){
If (isoutblankdir & basepath! = NULL ){
Zo. putnextentry (New zipentry (pathname + "/"); // empty directory can also be put in
}
If (isrecursive) {// Recursion
Zip (files [I]. getpath (), basepath, Zo, isrecursive, isoutblankdir );
}
} Else {
Fileinputstream fin = new fileinputstream (files [I]);
Zo. putnextentry (New zipentry (pathname ));
While (LEN = fin. Read (BUF)> 0 ){
Zo. Write (BUF, 0, Len );
}
Fin. Close ();
}
}
}
Public static Boolean indexertozip (string frompath, string tobasepath, string zipname)
{
If (! New file (frompath). isdirectory () return false;
Outputstream OS;
Try {
Tobasepath = filepath. fixpath (tobasepath );
If (! New file (tobasepath). exists ())
New file (tobasepath). mkdirs ();
String savepath = new file (tobasepath). getparent () + "/" + zipname + date_format.format (calendar. getinstance (). gettime () + ". Zip ";
If (! New file (savepath). exists () New file (savepath). createnewfile ();
OS = new fileoutputstream (savepath );
Bufferedoutputstream BS = new bufferedoutputstream (OS );
Zipoutputstream Zo = new zipoutputstream (BS );
Zip (frompath, new file (frompath), Zo, true, true );
Zo. closeentry ();
Zo. Close ();
Return true;
} Catch (exception e ){
Log. debug ("indexertozip err:" + E. getmessage ());
Return false;
}
}
}