Commons-compress operation zip file (compression and decompression)

Source: Internet
Author: User
Tags zip

Commons-compress: For more information about zip files (compression and decompression), see.

Apache Commons Compress is a class library for compressing and decompressing files.

You can operate files in ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200 and bzip2 formats with powerful functions.

Here, we will write two methods to Compress files into zip files and decompress files from zip files using Commons Compress.

Paste the tool code directly.

 

The code is as follows: Copy code

Package cn. luxh. utils;

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 org. apache. commons. compress. archivers. ArchiveEntry;

Import org.apache.commons.compress.archivers.zip. Zip64Mode;

Import org.apache.commons.compress.archivers.zip. ZipArchiveEntry;

Import org.apache.commons.compress.archivers.zip. ZipArchiveInputStream;

Import org.apache.commons.compress.archivers.zip. ZipArchiveOutputStream;

 

 

/**

* Zip file tool

* @ Author Luxh

*/

Public class ZipFileUtil {

 

/**

* Compressing a file into zip format

* @ Param files the file to be compressed

* @ Param zipFilePath: zip file path after compression, for example, "D:/test/aa.zip ";

*/

Public static void compressFiles2Zip (File [] files, String zipFilePath ){

If (files! = Null & files. length> 0 ){

If (isEndsWithZip (zipFilePath )){

ZipArchiveOutputStream zaos = null;

Try {

File zipFile = new File (zipFilePath );

Zaos = new ZipArchiveOutputStream (zipFile );

// Use Zip64 extensions for all entries where they are required

Zaos. setUseZip64 (Zip64Mode. AsNeeded );

 

// Encapsulate each file using ZipArchiveEntry

// Use ZipArchiveOutputStream to write to the compressed file

For (File file: files ){

If (file! = Null ){

ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry (file, file. getName ());

Zaos. putArchiveEntry (zipArchiveEntry );

InputStream is = null;

Try {

Is = new FileInputStream (file );

Byte [] buffer = new byte [1, 1024*5];

Int len =-1;

While (len = is. read (buffer ))! =-1 ){

// Write the buffer byte to ZipArchiveEntry

Zaos. write (buffer, 0, len );

}

// Writes all necessary data for this entry.

Zaos. closeArchiveEntry ();

} Catch (Exception e ){

Throw new RuntimeException (e );

} Finally {

If (is! = Null)

Is. close ();

}

 

}

}

Zaos. finish ();

} Catch (Exception e ){

Throw new RuntimeException (e );

} Finally {

Try {

If (zaos! = Null ){

Zaos. close ();

}

} Catch (IOException e ){

Throw new RuntimeException (e );

}

}

 

}

 

}

 

}

 

/**

* Decompress the zip file to the specified folder.

* @ Param zipFilePath: zip file path, for example, "D:/test/aa.zip"

* @ Param saveFileDir: the path of the extracted file, for example, "D:/test /"

*/

Public static void decompressZip (String zipFilePath, String saveFileDir ){

If (isEndsWithZip (zipFilePath )){

File file = new File (zipFilePath );

If (file. exists ()){

InputStream is = null;

// Can read Zip archives

ZipArchiveInputStream zais = null;

Try {

Is = new FileInputStream (file );

Zais = new ZipArchiveInputStream (is );

ArchiveEntry archiveEntry = null;

// Read each file in the zip package

// Then write the file to the specified folder

While (archiveEntry = zais. getNextEntry ())! = Null ){

// Get the file name

String entryFileName = archiveEntry. getName ();

// Construct the decompressed file storage path

String entryFilePath = saveFileDir + entryFileName;

Byte [] content = new byte [(int) archiveEntry. getSize ()];

Zais. read (content );

OutputStream OS = null;

Try {

// Write the extracted file to the specified path

File entryFile = new File (entryFilePath );

OS = new FileOutputStream (entryFile );

OS. write (content );

} Catch (IOException e ){

Throw new IOException (e );

} Finally {

If (OS! = Null ){

OS. flush ();

OS. close ();

}

}

 

}

} Catch (Exception e ){

Throw new RuntimeException (e );

} Finally {

Try {

If (zais! = Null ){

Zais. close ();

}

If (is! = Null ){

Is. close ();

}

} Catch (IOException e ){

Throw new RuntimeException (e );

}

}

}

}

}

 

/**

* The. Zip file name is used as the suffix.

* @ Param fileName the file name to be determined

* @ Return: return true for a zip file; otherwise, return false.

*/

Public static boolean isEndsWithZip (String fileName ){

Boolean flag = false;

If (fileName! = Null &&! "". Equals (fileName. trim ())){

If (fileName. endsWith (". ZIP") | fileName. endsWith (". zip ")){

Flag = true;

}

}

Return flag;

}

 

}

 

 

 

 


Package cn. luxh. test;

 

Import java. io. File;

 

Import org. junit. Test;

 

Import cn. luxh. utils. ZipFileUtil;

 

/**

* Test ZipFileUtil compression and decompression methods.

* @ Author Luxh

*/

Public class ZipFileUtilTest {

 

@ Test

Public void testCompressFiles2Zip (){

// Directory for storing the files to be compressed

File srcFile = new File ("D:/test ");

// Zip file path after compression

String zipFilePath = "d:/test2/test.zip ";

If (srcFile. exists ()){

File [] files = srcFile. listFiles ();

ZipFileUtil. compressFiles2Zip (files, zipFilePath );

}

}

 

@ Test

Public void testDecompressZip (){

// Path of the compressed package

String zipFilePath = "d:/test2/test.zip ";

// The directory where the extracted files are stored

String saveFileDir = "d:/test2 /";

// Call the decompression method

ZipFileUtil. decompressZip (zipFilePath, saveFileDir );

}

}

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.