Example of writing a compressed file in Java

Source: Internet
Author: User

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

}
}

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.