Decompress/decompress a file or a file in a folder using the java ZipOutputStream class

Source: Internet
Author: User

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. io. InputStream;
Import java. io. OutputStream;
Import java. util. ArrayList;
Import java. util. Enumeration;
Import java. util. List;
Import java.util.zip. ZipEntry;
Import java.util.zip. ZipFile;
Import java.util.zip. ZipOutputStream;
Import java.util.zip. GZIPInputStream;
Import java. io. DataInputStream;

Public class Zips {
/**
* Test the zip compression function. compress all files in the d: \ temp \ zipout directory along with subdirectories to d: \ temp \ out.zip.
*
* @ Param baseDir: name of the directory to be compressed (including the absolute path)
* @ Param objFileName the compressed file name
* @ Throws Exception
*/
Public void createZip (String baseDir, String objFileName) throws Exception {
File folderObject = new File (baseDir );
If (folderObject. exists ()){
List fileList = getSubFiles (new File (baseDir ));

// Compress the file name
ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (objFileName ));

ZipEntry ze = null;
Byte [] buf = new byte [1, 1024];
Int readLen = 0;
For (int I = 0; I <fileList. size (); I ++ ){
File f = (File) fileList. get (I );
System. out. println ("Adding:" + f. getPath () + f. getName ());

// Create a ZipEntry and set the Name and other attributes
Ze = new ZipEntry (getAbsFileName (baseDir, f ));
Ze. setSize (f. length ());
Ze. setTime (f. lastModified ());

// Add ZipEntry to zos and then write the actual file content
Zos. putNextEntry (ze );
InputStream is = new BufferedInputStream (new FileInputStream (f ));
While (readLen = is. read (buf, 0, 1024 ))! =-1 ){
Zos. write (buf, 0, readLen );
}
Is. close ();
System. out. println ("done ...");
}
Zos. close ();
} Else {
Throw new Exception ("this folder isnot exist! ");
}
}
/**
* Test the zip compression function. compress the specified file and save it to a compressed file.
*
* @ Param baseDir: name of the file to be compressed
* @ Param objFileName the compressed file name
* @ Return the size of the compressed file
* @ Throws Exception
*/
Public long createFileToZip (String zipFilename, String sourceFileName) throws Exception {

File sourceFile = new File (sourceFileName );
Byte [] buf = new byte [1, 1024];

// Compress the file name
File objFile = new File (zipFilename );
ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (objFile ));
ZipEntry ze = null;

// Create a ZipEntry and set the Name and other attributes
Ze = new ZipEntry (sourceFile. getName ());
Ze. setSize (sourceFile. length ());
Ze. setTime (sourceFile. lastModified ());

// Add ZipEntry to zos and then write the actual file content
Zos. putNextEntry (ze );
InputStream is = new BufferedInputStream (new FileInputStream (sourceFile ));
Int readLen =-1;
While (readLen = is. read (buf, 0, 1024 ))! =-1 ){
Zos. write (buf, 0, readLen );
}
Is. close ();
Zos. close ();
Return objFile. length ();
}
/**
* Test the decompression function. Decompress d: \ download \ test.zip along with the subdirectory to the d: \ temp \ zipout directory.
*
* @ Throws Exception
*/
Public void releaseZipToFile (String sourceZip, String outFileName) throws IOException {
ZipFile zfile = new ZipFile (sourceZip );
System. out. println (zfile. getName ());
Enumeration zList = zfile. entries ();
ZipEntry ze = null;
Byte [] buf = new byte [1, 1024];
While (zList. hasMoreElements ()){
// Obtain a ZipEntry from ZipFile
Ze = (ZipEntry) zList. nextElement ();
If (ze. isDirectory ()){
Continue;
}
// Obtain an InputStream with the ZipEntry parameter and write it to OutputStream.
OutputStream OS = new BufferedOutputStream (new FileOutputStream (getRealFileName (outFileName, ze. getName ())));
InputStream is = new BufferedInputStream (zfile. getInputStream (ze ));
Int readLen = 0;
While (readLen = is. read (buf, 0, 1024 ))! =-1 ){
OS. write (buf, 0, readLen );
}
Is. close ();
OS. close ();
System. out. println ("Extracted:" + ze. getName ());
}
Zfile. close ();
}
/**
* Obtain the list of all objects in the specified directory, including subdirectories.
*
* @ Param baseDir
* Directory specified by File
* @ Return List containing java. io. File
*/
Private List getSubFiles (File baseDir ){
List ret = new ArrayList ();
// File base = new File (baseDir );
File [] tmp = baseDir. listFiles ();
For (int I = 0; I <tmp. length; I ++ ){
If (tmp [I]. isFile ()){
Ret. add (tmp [I]);
}
If (tmp [I]. isDirectory ()){
Ret. addAll (getSubFiles (tmp [I]);
}
}
Return ret;
}
/**
* If the root directory is specified, the actual file name corresponding to a relative path is returned.
*
* @ Param baseDir
* Specify the root directory
* @ Param absFileName
* Relative Path name, from name in ZipEntry
* @ Return java. io. File actual File
*/
Private File getRealFileName (String baseDir, String absFileName ){
String [] dirs = absFileName. split ("/");
// System. out. println (dirs. length );
File ret = new File (baseDir );
// System. out. println (ret );
If (dirs. length> 1 ){
For (int I = 0; I <dirs. length-1; I ++ ){
Ret = new File (ret, dirs [I]);
}
}
If (! Ret. exists ()){
Ret. mkdirs ();
}
Ret = new File (ret, dirs [dirs. length-1]);
Return ret;
}
/**
* If the root directory is specified, the relative path of another file name is returned for the zip file.
*
* @ Param baseDir
* Java. lang. String root directory
* @ Param realFileName
* Actual File name of java. io. File
* @ Return relative file name
*/
Private String getAbsFileName (String baseDir, File realFileName ){
File real = realFileName;
File base = new File (baseDir );
String ret = real. getName ();
While (true ){
Real = real. getParentFile ();
If (real = null)
Break;
If (real. equals (base ))
Break;
Else {
Ret = real. getName () + "/" + ret;
}
}
System. out. println ("K" + ret );
Return ret;
}

Public static void main (String args []) {
Zips manager = new Zips ();
Try {
// Manager. createFileToZip ("d: \ temp \ out.zip ","");
Manager. releaseZipToFile ("d: \ temp \ out.zip", "d: \ test ");
// Manager. createZip ("d: \ temp \ zipout", "d: \ temp \ out.zip ");
}
Catch (Exception e ){
E. printStackTrace ();
}
System. out. println ("END ");
}

}

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.