Java Unzip File uncompress(String inputFile, String toDir)

Here you can find the source of uncompress(String inputFile, String toDir)

Description

uncompress

License

Open Source License

Declaration

public static void uncompress(String inputFile, String toDir) throws ZipException 

Method Source Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;

public class Main {
    private static final int BUFFER = 2048;

    public static void uncompress(String inputFile, String toDir) throws ZipException {
        try {/*from w w w  .ja  v a  2s  .co m*/
            FileInputStream tin = new FileInputStream(inputFile);
            CheckedInputStream cin = new CheckedInputStream(tin, new CRC32());
            BufferedInputStream bufferIn = new BufferedInputStream(cin, BUFFER);
            ZipInputStream in = new ZipInputStream(bufferIn);
            ZipEntry z = in.getNextEntry();

            while (z != null) {
                String name = z.getName();
                if (z.isDirectory()) {
                    File f = new File(toDir + File.separator + name);
                    f.mkdir();
                } else {
                    File f = new File(toDir + File.separator + name);
                    f.createNewFile();
                    FileOutputStream out = new FileOutputStream(f);
                    byte data[] = new byte[BUFFER];
                    int b;

                    while ((b = in.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, b);
                    }
                    out.close();
                }
                z = in.getNextEntry();
            }

            in.close();
        } catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }
}

Related

  1. uncompress(final InputStream inputStream)
  2. uncompress(final String zipFileName, final String directory)
  3. uncompress(String chemin, String tempPrefix)
  4. uncompress(String compressedValue)
  5. uncompress(String inputCompressedFile, File outputFile)
  6. uncompress(String str)
  7. uncompress(ZipInputStream zipIn)
  8. uncompressDirectory(File zipSource, File target)
  9. uncompressEveryFileFromDirectory(File srcPath, File dstPath)