Java ZIP file Compression

Source: Internet
Author: User

Package client;

Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java.util.zip. zipentry;
Import java.util.zip. zipoutputstream;

/**
* Compression implementation class <br>
* Main Implementation: <br>
* <P>
* Compressing a single file,
* <P>
* Compressing all files and subfolders in a folder
*
* <P>
*
* @ Author Libo
*
*/

Public class ziputil {

/**
* Compressing a single file
*
* @ Param filepath
* File path
* @ Param filename
* File name
* @ Param objdir
* Target folder of the compressed file
* @ Param ojbzipname
* Compressed file name
* @ Return
* @ Throws ioexception
*/
Public Boolean zip (string filepath, string filename, string objdir,
String ojbzipname) throws ioexception {
Boolean tag = false;
Zipoutputstream ZOS = NULL;
Fileinputstream FCM = NULL;
Try {
ZOS = new zipoutputstream (New fileoutputstream (objdir + ojbzipname ));

FS = new fileinputstream (filepath + filename );
Byte [] B = new byte [4096];
Int I = 0;

ZOS. putnextentry (New zipentry (filename ));
While (I = (FS. Read (B)> 0 ){
ZOS. Write (B, 0, I );
}
} Finally {
Try {
FCM. Close ();
} Catch (ioexception IOE ){
IOE. printstacktrace ();
Throw new ioexception ();
}
Try {
ZOS. Close ();
} Catch (ioexception IOE ){
IOE. printstacktrace ();
Throw new ioexception ();
}
}
Return tag;
}

/**
* Compress all files in a folder. Note that "/" is used for intermediate path connection, for example, C:/TT/TTT.
*
* @ Param srcpath
* Path of the folder to be compressed
* @ Param objzippath
* Target folder path
* @ Param ojbzipname
* Target compressed file name
* @ Return
*/
Public Boolean zipdir (string srcpath, string objzippath, string ojbzipname)
Throws exception {

Zipoutputstream ZOS = NULL;
Try {
File objfile = new file (objzippath, ojbzipname );
ZOS = new zipoutputstream (New fileoutputstream (objfile ));

If (srcpath = NULL ){
System. Out. println ("the input source folder path string cannot be blank! ");
Throw new java. Lang. nullpointerexception ();
}
String dirname = "";
File file = new file (srcpath );
If (! File. isdirectory ()){
Throw new exception ("Incorrect source folder path passed in! ");
} Else {
Dirname = srcpath. substring (srcpath. lastindexof ("/") + 1 );
If (dirname = NULL | "". Equals (dirname )){
String substr = srcpath. substring (0, srcpath. Length ()-2 );
Dirname = substr. substring (substr. lastindexof ("/") + 1 );
}
Zipentry ze = new zipentry (dirname + "/");
ZOS. putnextentry (ze );
If (dirname = NULL | "". Equals (dirname )){
Throw new exception ("Incorrect source folder path passed in! ");
}
}

File [] files = file. listfiles ();

For (INT I = 0; I <files. length; I ++ ){
Zipfile (dirname + "/", files [I], ZOS );
}
Return true;
} Finally {
If (ZOS! = NULL ){
Try {
ZOS. Close ();
} Catch (ioexception E1 ){
E1.printstacktrace ();
}
}
}

}

/**
* Used to compress all sub-files in a folder. This method is a recursive method.
*
* @ Param zippath
* Relative Path of the file to be compressed in the compressed package
* @ Param File
* File reference to be compressed
* @ Param ZOS
* Compressed file output stream
* @ Throws ioexception
*/
Private void zipfile (string zippath, file, zipoutputstream ZOS)
Throws ioexception {
// Folder operations
If (file. isdirectory ()){
Zipentry ze = new zipentry (zippath + file. getname () + "/");
ZOS. putnextentry (ze );
// Recursive call
For (INT I = 0; I <file. listfiles (). length; I ++ ){
Zipfile (zippath + file. getname () + "/", file. listfiles () [I],
ZOS );
}
// Operation when the file is used
} Else {
Fileinputstream FCM = NULL;
Try {
FS = new fileinputstream (File );
Zipentry ze = new zipentry (zippath + file. getname ());
ZOS. putnextentry (ze );
Byte [] B = new byte [4096];
Int I = 0;
While (I = (FS. Read (B)> 0 ){
ZOS. Write (B, 0, I );
}
} Finally {
If (FS! = NULL ){
FCM. Close ();
}
}
}
}

Public static void main (string [] ARGs ){
Ziputil zt = new ziputil ();
Try {
Zt.zip Dir ("C:/GIF", "C:/", "test.zip ");
} Catch (exception e ){
E. printstacktrace ();
}
}

}

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.