Android Zip Unzip File decompress(File file)

Here you can find the source of decompress(File file)

Description

decompress

Declaration

public static void decompress(File file) throws Exception 

Method Source Code


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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 java.util.zip.GZIPInputStream;

public class Main {
    public static final int BUFFER = 1024;
    public static final String EXT = ".gz";

    public static byte[] decompress(byte[] data) {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        /*from   w  ww .j  av a 2  s  .  co m*/

        decompress(bais, baos);

        data = baos.toByteArray();

        try {
            baos.flush();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    
                }
            }

            if (bais != null) {
                try {
                    bais.close();
                } catch (IOException e) {
                    
                }
            }

        }

        return data;
    }

    public static void decompress(File file) throws Exception {
        decompress(file, true);
    }

    public static void decompress(File file, boolean delete)
            throws Exception {
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file.getPath().replace(
                EXT, ""));
        decompress(fis, fos);
        fis.close();
        fos.flush();
        fos.close();

        if (delete) {
            file.delete();
        }
    }

    public static void decompress(InputStream is, OutputStream os) {

        GZIPInputStream gis = null;
        try {
            gis = new GZIPInputStream(is);
            int count;
            byte data[] = new byte[BUFFER];
            while ((count = gis.read(data, 0, BUFFER)) != -1) {
                os.write(data, 0, count);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if (gis != null) {
                try {
                    gis.close();
                } catch (IOException e) {
                    
                }
            }
        }
    }

    public static void decompress(String path) throws Exception {
        decompress(path, true);
    }

    public static void decompress(String path, boolean delete)
            throws Exception {
        File file = new File(path);
        decompress(file, delete);
    }
}

Related

  1. zip(File srcFile, File targetFile)
  2. zip(File[] _files, File _zipFile)
  3. zip(List listFiles, String destZipFilePath)
  4. zipFile(File source, String basePath, ZipOutputStream zos)
  5. zipFileAtPath(File sourceFile, String toLocation)
  6. decompress(File file)
  7. decompress(File file, boolean delete)
  8. decompress(File file, boolean delete)
  9. decompress(String paramString)