Java zip/gzip file compression and decompression __java

Source: Internet
Author: User
Tags gz file
Java IO

In order to reduce the amount of data transmitted in Java, a special compression stream is provided to compress a file or folder into a Zip,gzip,jar file format. Compression Stream Implementation

Three compressed formats supported by Java: Zip, jar, gzip.

1.zip is a more common compression format, Java provides a java.util.zip package, common classes: Zipinputstream zipoutputstream zipentry ZipFile

Rely on the above 4 classes to complete the zip type file operation.

2.Jar is a Java-proprietary compression format, Java provides a Java.util.jar package, common classes: Jarinputstream jaroutputstream jarentry Jarfile


3.GZIP is a file compression format for UNIX systems, and the frequently used. gz file in Linux systems is the gzip format, and Java provides gzipinputstream Gzipoutputstream


This paper takes zip compression/decompression as an example to explain. Compress

@Test public
void Testzip () throws IOException {file

    zipfile = new File ("D:\\log.zip");//Compressed Files
    Zipoutputstream zipout = null;
    try{
        zipout = new Zipoutputstream (new FileOutputStream (ZipFile));
        File dir = new file ("d:\\logs\\info\\");   Folders to be compressed
        file[] files = dir.listfiles ();
        for (File file:files) {
            inputstream in = null;
            try{
                zipout.putnextentry (New ZipEntry (File.getname ()));
                Zipout.setcomment ("Test zip");  Set Note
                in = new FileInputStream (file);//define Input stream
                ioutils.copy (in, zipout);
            } finally {
                ioutils.closequietly (in);}}}
    finally {
        ioutils.closequietly (zipout);
    }

    SYSTEM.OUT.PRINTLN ("Compressed file Complete");
}
Decompression
@Test public void Testunzip () throws IOException {file dir = new file ("D:\\test");
    if (!dir.exists ()) {dir.mkdirs ();
    The file Srcfile = new file ("D:\\log.zip");

    ZipFile zipfile = new ZipFile (srcfile);
    Zipinputstream zipin = null;
        try{Zipin = new Zipinputstream (new FileInputStream (srcfile));
        ZipEntry entry = null;
            while ((entry = Zipin.getnextentry ())!=null) {System.out.println ("Extract file:" + entry.getname ());
            OutputStream out = null;
            InputStream in = null;
                try{File File = new file (dir, Entry.getname ());
                in = Zipfile.getinputstream (entry);
                out = new FileOutputStream (file);
            Ioutils.copy (in, out);
                }finally {ioutils.closequietly (out);
            Ioutils.closequietly (in);
    }}}finally {ioutils.closequietly (zipin);
} System.out.println ("Extract file Complete"); }

Ioutils.java

Import java.io.Closeable;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.OutputStream; /** * ${description} * * @author Ricky Fung * @create 2016-10-31 10:19 * * public class Ioutils {public static V
    OID closequietly (InputStream input) {closequietly (closeable) input);
    public static void closequietly (OutputStream output) {closequietly (closeable) output);
                public static void closequietly (Closeable closeable) {try {if (closeable!= null) {
            Closeable.close ();
        } catch (IOException e) {e.printstacktrace ();
            } public static void Closequietly (Closeable ... closeables) {for (closeable closeable:closeables) {
        closequietly (closeable);  public static long copy (InputStream in, outputstream out, int buffersize) throws IOException {byte[]
        Buff = new Byte[buffersize]; return copy (in, out, buff); public static long copy (InputStream in, outputstream out) throws IOException {byte[] buff = new byte[1024]
        ;
    return copy (in, out, buff); 
        public static long copy (InputStream in, outputstream out, byte[] buff) throws IOException {Long Count = 0;
        int len =-1;
            while ((Len=in.read (buff, 0, buff.length))!=-1) {out.write (buff, 0, Len);
        Count = Len;
    return count; }
}

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.