Java zip compression optimized version solves the problem of files being occupied and cannot be deleted after compression, javazip

Source: Internet
Author: User

Java zip compression optimized version solves the problem of files being occupied and cannot be deleted after compression, javazip

Recently, I tried zip operations and found a solution from the Internet. However, some bugs have been found in the experiment, mainly because there are problems with the declaration of the file stream. As a result, the jvm keeps occupying files without releasing them, specially send your modified records and keep the records.

Import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java.util.zip. zipEntry; import java.util.zip. zipOutputStream; public class Zippic {public final static class FileToZip {private FileToZip () {}/ *** will be stored in sourceFilePath The source file under the directory is packaged into a ZIP file named fileName and stored in zipFilePath. * @ Param sourceFilePath path of the file to be compressed * @ param zipFilePath: storage path after compression * @ param fileName name of the compressed file * @ return flag */public static boolean fileToZip (String sourceFilePath, string zipFilePath, String fileName) {boolean flag = false; File sourceFile = new File (sourceFilePath); if (sourceFile. exists () = false) {System. out. println (">>>>>> directory of the file to be compressed:" + sourceFilePath + "does not exist. <"); flag = false; return flag;} e Lse {try {File zipFile = new File (zipFilePath + "/" + fileName + ". zip "); if (zipFile. exists () {System. out. println (">>>>>>" + zipFilePath + "the directory name is:" + fileName + ". zip "+" package file. <");} else {File [] sourceFiles = sourceFile. listFiles (); if (null = sourceFiles | sourceFiles. length <1) {System. out. println (">>>>>> directory of the file to be compressed:" + sourceFilePath + "there is no file in it, no compression is required. <"); flag = false; r Eturn flag;} else {ZipOutputStream zos = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile); // declare it only when it is in use; otherwise it is prone to problems. Remember to turn it off first, then turn off byte [] bufs = new byte [1024*10]; // buffer block for (int I = 0; I <sourceFiles. length; I ++) {// create a ZIP object and add it to the compressed package ZipEntry zipEntry = new ZipEntry (sourceFiles [I]. getName (); zos. putNextEntry (zipEntry); // read the file to be compressed and write it into the compressed package BufferedInputStream bis = new BufferedInputStream (New FileInputStream (sourceFiles [I]), 1024*10); // declare it only when it is used. Otherwise, problems may occur. Remember to turn it off first, and then turn it off int read = 0; while (read = (bis. read (bufs, 0, 1024*10 )))! =-1) {zos. write (bufs, 0, read);} if (null! = Bis) bis. close (); // close} flag = true; if (null! = Zos) zos. close (); // close }}catch (FileNotFoundException e) {e. printStackTrace (); throw new RuntimeException (e);} catch (IOException e) {e. printStackTrace (); throw new RuntimeException (e) ;}} return flag ;}}}

 


During Java zip compression, the disk read/write count is high and the speed is slow. Can I add a buffer to solve this problem?

The core of java io stream is the Decorator mode ):

ZipOutputStream out = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (

ZipFileName )));

In this way, you can enjoy the write cache.

Java compression folder. If you have any questions, it will always fail to be compressed.

Package com. emis. util;

Import org.apache.tools.zip. ZipEntry;
Import org.apache.tools.zip. ZipOutputStream;

Import java. io .*;

/**
* User: zhong. xu
* Date: 2009-3-5
* Time: 9:27:45
*
* Package files or folders into ZIP files
*
*/
Public class emisZipUtil extends ZipOutputStream {

Public emisZipUtil (OutputStream outputStream ){
This (outputStream, defaultEncoding, defaultLevel );
}

Public emisZipUtil (String file) throws IOException {
This (new FileOutputStream (new File (file), defaultEncoding, defaultLevel );
}

Public emisZipUtil (File file) throws IOException {
This (new FileOutputStream (file), defaultEncoding, defaultLevel );
}

/**
* Unified calling of Constructor
*
* @ Param outputStream output stream (output path), *. zip
* @ Param encoding
* @ Param level compression level 0-9
**/
Public emisZipUtil (OutputStream outputStream, String encoding, int level ){
Super (outputStream );

Buf = new byte [1024]; // 1024 KB buffer

If (encoding! = Null |! "". Equals (encoding ))
This. setEncoding (encoding );

If (level <0 | level> 9) level = 7;
This. setLevel (level );

Comment = new StringBuffer ();
}

Public String put (String fileName) throws IOException {
Return put (fileName ,"");
}

/**
* Add files or folders to be compressed
*
* @ Param fileName Add a file or a folder
* @ Param pathName: folder path added when ZIP is generated
* @ Return fileName
**/
Public String put (String fileName, String pathName) throws IOException {
File file = new File (fileName );

If... the remaining full text>

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.