Using Java to achieve compression and decompression (zip.gzip) support Chinese path

2010-11-03  来源:本站原创  分类:Mobile  人气:202 

zip archive and compression play two roles; gzip archive file is not only just a single file compression, so in a UNIX platform, the command normally used to create a tar archive file, and then to the file command gzip file compression .

Java I / O library also contains a compressed format that can read and write streams. To provide compression, as long as they are wrapped in an existing I / O on the line outside of class. These classes are not Reader and Writer, but a subclass of InputStream and OutStreamput. This is because the compression algorithm is not the character for the byte.

Related classes and interfaces:
Checksum Interface: The class interface implemented Adler32 and CRC32
Adler32: Use Alder32 algorithm to calculate the number Checksum
CRC32: CRC32 algorithm used to calculate the number Checksum

CheckedInputStream: InputStream-derived class, get the input stream checksum Checksum, to verify data integrity
CheckedOutputStream: OutputStream derived class, get the output stream checksum Checksum, to verify data integrity

DeflaterOutputStream: compressing the base class.
ZipOutputStream: DeflaterOutputStream a sub-category, the data compressed into a Zip file format.
GZIPOutputStream: DeflaterOutputStream a sub-category, the data into a GZip compressed file format

InflaterInputStream: Extract the base class
ZipInputStream: InflaterInputStream a subclass, can decompress Zip format data
GZIPInputStream: InflaterInputStream a subclass, can decompress Zip format data

ZipEntry class: that ZIP file entries
ZipFile class: This class is used to read entries from a ZIP file

Conducted on a single file with the GZIP compression
GZIP interface is simple, so if you only need to compress a stream, you can use it. Of course, it can compress the character stream, and can compress the byte stream, the following is an GBK encoding format for compressed text files.
Usage is very simple compression type; As long as the output stream ZipOutputStream GZIPOutputStream or wrap, and then GZIPInputStream or ZipInputStream wrap the input stream on the line. Leaving behind the common I / O operations.

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPcompress {
        public static void main(String[] args) throws IOException {
                //  Compressed file to prepare for a character, note  ,  If this document GBK character encoding
                BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(
                                "e:/tmp/source.txt"), "GBK"));
                //  Use GZIPOutputStream packaging  OutputStream  Flow characteristics to the specific compression  ,  Last generated test.txt.gz archive
                //  And which has a file named test.txt
                BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(
                                new FileOutputStream("test.txt.gz")));
                System.out.println("  Began to write compressed files  ...");
                int c;
                while ((c = in.read()) != -1) {

                        /*
                         *   Note, this is a character file compression  ,  Is a character stream to read the front, and not directly into  c,  Because c is  Unicode
                         *   Code, this information will be lost  (  Encoding format itself is not of course  ),  So here again to be credited to GBK to solve  .
                         */
                        out.write(String.valueOf((char) c).getBytes("GBK"));
                }
                in.close();
                out.close();
                System.out.println("  Started to read compressed files  ...");
                //  Use GZIPInputStream packaging  InputStream  Flow, to extract features with
                BufferedReader in2 = new BufferedReader(new InputStreamReader(
                                new GZIPInputStream(new FileInputStream("test.txt.gz")), "GBK"));
                String s;
                //  Read the contents of compressed files
                while ((s = in2.readLine()) != null) {
                        System.out.println(s);
                }
                in2.close();
        }
}

Zip file compression using multiple
Java class library support for Zip format was more comprehensive, starting a number of files that can be compressed into a compact package. This library uses the standard Zip format, so the compression tool with many compatible.

ZipOutputStream class has set the compression method and compression, the compression level to use, zipOutputStream.setMethod (int method) Set the default compression method for entry. As long as a single ZIP file entry does not specify the compression method to use the compression method set ZipOutputStream to store, the default is ZipOutputStream.DEFLATED (that is compressed storage), you can also set STORED (that only the package archive storage). ZipOutputStream set the compression method DEFLATED, we can further use setLevel (int level) method to set the compression level, compression level is 0-9 a total of 10 levels (higher the value, expressed more interest in compression), default Deflater.DEFAULT_COMPRESSION =- 1. Of course, we can also ZipEntry entry method for a single condition of setMethod set compression method.

Class ZipEntry describes the file stored in the ZIP compressed files. Class includes a variety of methods can be used to set and get ZIP item. ZipEntry class is ZipFile [zipFile.getInputStream (ZipEntry entry)] and used to read ZIP file ZipInputStream, ZipOutputStream to write ZIP files. Following these useful methods: getName () Returns the entry name, isDirectory () if directory entries, it returns true (the definition of its name directory entry '/' at the end of the entry), setMethod (int method) Set the compression entries method for ZipOutputStream.STORED or ZipOutputStream. DEFLATED.

The following example we use apache's zip kit (where the package is ant.jar), because of the type that comes with java does not support Chinese path, but both use the same way, but apache compression tool set encoding more interface, the other is basically the same. Also, if you use org.apache.tools.zip.ZipOutputStream to compress, we can only use org.apache.tools.zip.ZipEntry to unpack, but can not be used to extract read java.util.zip.ZipInputStream , of course, did not provide apache ZipInputStream class.

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.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
 *
 *  Provides a single file and directory compression, and supports the need to create a compressed source directory  .  Chinese path
 *
 * @author jzj
 */
public class ZipCompress {

        private static boolean isCreateSrcDir = true;//  Whether to create the source directory  

        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                String src = "m:/  New text document  .txt";//  Specifies the compression source, can be a directory or file
                String decompressDir = "e:/tmp/decompress";//  Extraction path
                String archive = "e:/tmp/test.zip";//  Archive path
                String comment = "Java Zip   Test  .";//  Notes archive  

                //----  Zip file or directory
                writeByApacheZipOutputStream(src, archive, comment);

                /*
                 *   Read compressed file, comment out  ,  Because using the compression type apache  ,  So using the java class library
                 *   An error occurred while extracting class, where can not run
                 */
                //readByZipInputStream();
                //----  Read the compressed file using apace ZipFile
                readByApacheZipFile(archive, decompressDir);
        }

        public static void writeByApacheZipOutputStream(String src, String archive,
                        String comment) throws FileNotFoundException, IOException {
                //----  Zip file  :
                FileOutputStream f = new FileOutputStream(archive);
                //  Check and create the specified output stream
                CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());

                ZipOutputStream zos = new ZipOutputStream(csum);
                //  Chinese support
                zos.setEncoding("GBK");
                BufferedOutputStream out = new BufferedOutputStream(zos);
                //  Set Notes archive
                zos.setComment(comment);
                //  Enable Compression
                zos.setMethod(ZipOutputStream.DEFLATED);
                //  Compression level for the most compression, but takes a little more time
                zos.setLevel(Deflater.BEST_COMPRESSION);

                File srcFile = new File(src);

                if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) {
                        throw new FileNotFoundException(
                                        "File must exist and  ZIP file must have at least one entry.");
                }
                //  Get the parent directory where the compressed source
                src = src.replaceAll("\\\\", "/");
                String prefixDir = null;
                if (srcFile.isFile()) {
                        prefixDir = src.substring(0, src.lastIndexOf("/") + 1);
                } else {
                        prefixDir = (src.replaceAll("/$", "") + "/");
                }

                //  If it is not the root directory
                if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) {
                        prefixDir = prefixDir.replaceAll("[^/]+/$", "");
                }

                //  Compact
                writeRecursive(zos, out, srcFile, prefixDir);

                out.close();
                //   Note :  Checksum in the stream is closed after the preparation, must be used on the stream is closed
                System.out.println("Checksum: " + csum.getChecksum().getValue());
                BufferedInputStream bi;
        }

        /**
         *   Unzip the file using org.apache.tools.zip.ZipFile  ,  It is with the java class library
         * java.util.zip.ZipFile   Is a new use, but more to set encoding
         *   Interface  .
         *
         *   Note, apache does not provide   ZipInputStream   Class, so only use it provides  ZipFile
         *   To read the compressed file  .
         * @param archive   Archive path
         * @param decompressDir   Extraction path
         * @throws IOException
         * @throws FileNotFoundException
         * @throws ZipException
         */
        public static void readByApacheZipFile(String archive, String decompressDir)
                        throws IOException, FileNotFoundException, ZipException {
                BufferedInputStream bi;

                ZipFile zf = new ZipFile(archive, "GBK");//  Chinese support  

                Enumeration e = zf.getEntries();
                while (e.hasMoreElements()) {
                        ZipEntry ze2 = (ZipEntry) e.nextElement();
                        String entryName = ze2.getName();
                        String path = decompressDir + "/" + entryName;
                        if (ze2.isDirectory()) {
                                System.out.println("  Extract the directory is being created   - " + entryName);
                                File decompressDirFile = new File(path);
                                if (!decompressDirFile.exists()) {
                                        decompressDirFile.mkdirs();
                                }
                        } else {
                                System.out.println("  Creating extract files   - " + entryName);
                                String fileDir = path.substring(0, path.lastIndexOf("/"));
                                File fileDirFile = new File(fileDir);
                                if (!fileDirFile.exists()) {
                                        fileDirFile.mkdirs();
                                }
                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
                                                decompressDir + "/" + entryName));

                                bi = new BufferedInputStream(zf.getInputStream(ze2));
                                byte[] readContent = new byte[1024];
                                int readCount = bi.read(readContent);
                                while (readCount != -1) {
                                        bos.write(readContent, 0, readCount);
                                        readCount = bi.read(readContent);
                                }
                                bos.close();
                        }
                }
                zf.close();
        }

        /**
         *   Java api in use   ZipInputStream   Class-extracting file, but if used compression
         * org.apache.tools.zip.ZipOutputStream  When, instead of   java   Class Library
         * java.util.zip.ZipOutputStream  , The method can not be used  ,  The reason is that coding side
         *   Type inconsistent result, the runtime will throw the following exception  :
         * java.lang.IllegalArgumentException
         * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:290)
         *
         *   Of course, if compressed using  java  Class library  java.util.zip.ZipOutputStream
         *   There is no problem from compression, but it does not support Chinese
         *
         * @param archive   Archive path
         * @param decompressDir   Extraction path
         * @throws FileNotFoundException
         * @throws IOException
         */
        public static void readByZipInputStream(String archive, String decompressDir)
                        throws FileNotFoundException, IOException {
                BufferedInputStream bi;
                //----  Unzip the file  (ZIP  Unzip the file is actually read from the input stream  ):
                System.out.println("  Started to read compressed files  ");

                FileInputStream fi = new FileInputStream(archive);
                CheckedInputStream csumi = new CheckedInputStream(fi, new CRC32());
                ZipInputStream in2 = new ZipInputStream(csumi);
                bi = new BufferedInputStream(in2);
                java.util.zip.ZipEntry ze;//  Zip file entry
                //  Traverse the compressed package file entries
                while ((ze = in2.getNextEntry()) != null) {
                        String entryName = ze.getName();
                        if (ze.isDirectory()) {
                                System.out.println("  Extract the directory is being created   - " + entryName);
                                File decompressDirFile = new File(decompressDir + "/" + entryName);
                                if (!decompressDirFile.exists()) {
                                        decompressDirFile.mkdirs();
                                }
                        } else {
                                System.out.println("  Creating extract files   - " + entryName);
                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
                                                decompressDir + "/" + entryName));
                                byte[] buffer = new byte[1024];
                                int readCount = bi.read(buffer);

                                while (readCount != -1) {
                                        bos.write(buffer, 0, readCount);
                                        readCount = bi.read(buffer);
                                }
                                bos.close();
                        }
                }
                bi.close();
                System.out.println("Checksum: " + csumi.getChecksum().getValue());
        }

        /**
         *   Recursive compression
         *
         *   Compressed using org.apache.tools.zip.ZipOutputStream class  ,  Its advantage is to support Chinese path  ,
         *   The Java Class Library   java.util.zip.ZipOutputStream   Compressed archive file name in Chinese will be garbled  .
         *   Use apache in this class   java   The use of class library is a new, just to set the encoding of the  .
         *
         * @param zos
         * @param bo
         * @param srcFile
         * @param prefixDir
         * @throws IOException
         * @throws FileNotFoundException
         */
        private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo,
                        File srcFile, String prefixDir) throws IOException, FileNotFoundException {
                ZipEntry zipEntry;

                String filePath = srcFile.getAbsolutePath().replaceAll("\\\\", "/").replaceAll(
                                "//", "/");
                if (srcFile.isDirectory()) {
                        filePath = filePath.replaceAll("/$", "") + "/";
                }
                String entryName = filePath.replace(prefixDir, "").replaceAll("/$", "");
                if (srcFile.isDirectory()) {
                        if (!"".equals(entryName)) {
                                System.out.println("  Creating directory   - " + srcFile.getAbsolutePath()
                                                + "  entryName=" + entryName);

                                //  If a directory, you need to add the words in the writing directory   /
                                zipEntry = new ZipEntry(entryName + "/");
                                zos.putNextEntry(zipEntry);
                        }

                        File srcFiles[] = srcFile.listFiles();
                        for (int i = 0; i < srcFiles.length; i++) {
                                writeRecursive(zos, bo, srcFiles[i], prefixDir);
                        }
                } else {
                        System.out.println("  Writing paper   - " + srcFile.getAbsolutePath() + "  entryName="
                                        + entryName);
                        BufferedInputStream bi = new BufferedInputStream(new FileInputStream(srcFile));

                        //  Start writing a new ZIP file entry and positions the stream at the beginning of the data entry
                        zipEntry = new ZipEntry(entryName);
                        zos.putNextEntry(zipEntry);
                        byte[] buffer = new byte[1024];
                        int readCount = bi.read(buffer);

                        while (readCount != -1) {
                                bo.write(buffer, 0, readCount);
                                readCount = bi.read(buffer);
                        }
                        //  Note, the use of the buffer stream to write compressed files  ,  A condition must be refreshed after a not
                        //  However, some content may be deposited to the back entry to the
                        bo.flush();
                        //  Close file after reading
                        bi.close();
                }
        }
}

To add the file compressed, you have to ZipEntry object passed putNextEntry (). ZipEntry is an interface very complicated object, it allows you to set and read Zip files of a record of information, which includes: file name, before compression and the compressed size, date, CRC check code, additional fields, annotations, compression method, if it is directory. Although the standard Zip format support password, but does not support Java's Zip library. ZipEntry CRC and provides an interface only, while CheckedInputStream and CheckedOutputStream are two kinds of support Adler32 and CRC32 checksum. Although this is the bottom of the Zip format restrictions, but it prevents you from using a faster Adler32.

To extract the files, you can use ZipInputStream the getNextEntry () method. Compression bag as long as there ZipEntry, it will extract it. There is also a more concise way, you can use to read the file ZipFile object. ZipFile one entries () method, which can return ZipEntries the Enumeration. Then zipFile. GetInputStream (ZipEntry entry) for the corresponding compressed stream can read the entries.

To read the check code, you must first obtain the Checksum object. Here we use the CheckedOutputStream and CheckedInputStream, but you can also use the Checksum. java.util.zip checksum algorithm in the package is more important class is Adler32 and CRC32, they realized java.util.zip.Checksum interface, and estimated the compressed data checksum (checksum). In computing speed, Adler32 algorithm than the CRC32 algorithm to have certain advantages; but in terms of data credibility, CRC32 algorithm will have better. GetValue method can be used to obtain the current checksum value, reset method can be reset to its default value of the checksum.

Generally used to verify the checksum file and the information is correct delivery. For example, suppose you want to create a ZIP file, and then sent to a remote computer. When you get to the remote computer, you can use the checksum test whether the file during transmission error occurs, a bit like downloading files we can use the hash value to verify the file download process is wrong.

Zip class has a astonishing setComment () method. If ZipCompress.java shows, write files, you can add comments, but when reading the file, ZipInputSream does not provide interfaces. It seems the comment feature is completely against the entry, is ZipEntry achieved.

Of course, GZIP and Zip compressed files can be used not only - it can compress anything, including data transmitted through the network.

相关文章
  • Using Java to achieve compression and decompression (zip.gzip) support Chinese path 2010-11-03

    zip archive and compression play two roles; gzip archive file is not only just a single file compression, so in a UNIX platform, the command normally used to create a tar archive file, and then to the file command gzip file compression . Java I / O l

  • java to achieve compression and decompression 2010-01-19

    package com.yss.test.FileReadWriter; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Enumeration; import java.util.zip.Zip

  • VB Shell to achieve compression and decompression (Zip or Rar) 2011-03-09

    Private Sub ZipOrRar () 'The C drive of the test folder compressed files test.rar Shell App.Path + "\ WinRar.exe MC: \ test.rar C: \ test" End Sub Private Sub UnZipOrRar () 'Extract the test.rar under the C drive Shell App.Path + "\ WinRar.

  • Compression and decompression Zip (PHPZip class) 2010-12-30

    PHPZip class Source: Internet visitFile () function Source: http://topic.csdn.net/u/20071225/11/e6a8db84-df02-4622-987b-2a4ed4a5eef0.html (14th floor) Note: modified to traverse the sub-folder (replace the traversal methods visitFile ()); Add GetZipC

  • Java compression order (3) ZIP decompression 2010-04-13

    JavaEye friend said to me: "you're going to have to compress and decompress ZIP write a post, I look tired, not as separate good read." ok, the demand for the readership, I made adjustments to that ZIP decompression alone here! Related links: Ja

  • java rar compression and decompression to achieve 2010-09-03

    public class RarToFile { /* * cmd Compression and decompression command */ private static String rarCmd = "C:\\Program Files\\WinRAR\\Rar.exe a "; private static String unrarCmd = "C:\\Program Files\\WinRAR\\UnRar x "; public static vo

  • Java compression order (2) ZIP 2010-04-13

    ZLib algorithm last year compiled a Java implementation (Java compression technology consolidation (1) ZLib), has been caring Quemei time to add. Today was empty, sort of java native ZIP achieved. Related links: Java compression order (1) ZLib Java c

  • tar zip package. compression and decompression methods to the specified directory 2010-03-25

    Tar packing, compression and decompression methods to the specified directory linux tar is commonly used in packaging, compression, compression tools, many of his arguments, fold in just listed common parameters of compression and decompression Param

  • PclZip: powerful PHP class zip compression and decompression 2010-05-06

    PclZip Introduction PclZip a very powerful zip file compression and decompression of PHP classes, PclZip library can compress and decompress Zip format compressed files (WinZip, PKZIP); and can deal with this type of class files, including compressed

  • Zip file compression and decompression 2010-06-17

    Zip file compression and decompression main methods: /// <summary> /// Create a compression method /// </summary> /// <param name="filesPath"> File directory </param> /// <param name="zipFilePath"> Save Di

  • linux the file compression and decompression: gzip, zip, bzip2 file compression and archiving (to) 2011-05-26

    14.3. File compression and archiving Sometimes, we need to be stored as a set of files for backup or transfer a file to another directory or even another computer. Sometimes, we also need to file compressed into a file, so they use only a small amoun

  • under linux file compression and decompression: gzip, zip, bzip2 file compression and archiving (rpm) 2011-05-26

    14.3 file compression and archiving Sometimes, we need to store a group of files into a file for backup or transfer to another directory or even another computer. Sometimes, we also need to file compressed into a file, so they use only a small amount

  • linux zip unzip compression and decompression command 2011-04-01

    zip is compressed under linux common commands, files, compressed by it will also produce a ". zip" extension of the compressed file, because zip can easily use windows, so the zip is a widely used compression program. Syntax: zip [-AcdDfFghjJKlL

  • Huffman tree + double buffering to achieve compression algorithm (Updated) 2010-03-29

    The first thing we know before the Huffman tree to find out the simple binary tree, it is easy to understand, is a binary tree each node have only two sub-nodes in the tree structure, and also divided into parent node (parent node), the left sub-tree

  • java zip file, the Chinese question 2009-10-17

    Today, the students asked them to do with java compression and decompression of the program, occurs in Chinese, I have done before, but has been a long time, where they did not write the log, so it forgot its own small program made by compression, to

  • Compression and decompression command under Unix 2010-09-13

    Example: . Tar Unpack: tar-xvf FileName.tar Package: tar-cvf FileName.tar DirName (Note: tar is packaged, not compressed!) --------------------------------------------- . Gz Extract 1: gunzip FileName.gz Extract 2: gzip-d FileName.gz Compression: gzi

  • Linux Tutorials --- Linux systems commonly used in compression and decompression command 2010-03-11

    Linux Tutorials --- Linux systems commonly used in compression and decompression command Articles Category: Operating system Linux common commands, such as compression and decompression as shown in Table 2-5. Table 2-5 Linux common compression and de

  • ubuntu command under the use of compression and decompression 2010-03-13

    . Tar Unpack: tar xvf FileName.tar Package: tar cvf FileName.tar DirName (Note: tar is packaged, not compressed!) --------------------------------------------- . Gz Extract 1: gunzip FileName.gz Extract 2: gzip-d FileName.gz Compression: gzip FileNam

  • compression and decompression under linux explicitly Daquan [change] 2010-04-08

    Identified so uncomfortable every time. Summarized here to facilitate Dora!!! General summary of what the various formats under linux archive compression and decompression method. But I did not use some method, also incomplete, I hope you help me to

  • compression and decompression under linux 2010-04-16

    compression and decompression under linux For access to Linux just for people who will give a lot of Linux, the file name to engage in a variety of halo. Do not say one, just on the compressed file, for example, we know that in Windows, the most comm