Android InputStream Unzip decompress(InputStream is, OutputStream os)

Here you can find the source of decompress(InputStream is, OutputStream os)

Description

decompress

License

Open Source License

Declaration

private static void decompress(InputStream is, OutputStream os) 

Method Source Code


import java.io.*;
import java.util.zip.GZIPInputStream;

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

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

        //from   ww w.  j  a v a 2  s.  co  m

        decompress(bais, baos);

        data = baos.toByteArray();

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

            try {
                bais.close();
            } catch (IOException e) {
                
            }

        }

        return data;
    }

    @SuppressWarnings("UnusedDeclaration")
    public static void decompress(File file) throws Exception {
        decompress(file, true);
    }

    private 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();
        }
    }

    private 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) {
                    
                }
            }
        }
    }

    @SuppressWarnings("UnusedDeclaration")
    public static void decompress(String path) throws Exception {
        decompress(path, true);
    }

    @SuppressWarnings("SameParameterValue")
    private static void decompress(String path, boolean delete)
            throws Exception {
        File file = new File(path);
        decompress(file, delete);
    }
}

Related

  1. unZip(InputStream in, String destDir)
  2. unZip(InputStream is, String filename, String folderPath)
  3. unpackZip(InputStream zipStream, String unpackPath)
  4. unzip(InputStream fileIn, File dirOut)
  5. decompress(InputStream is, OutputStream os)
  6. decompress(InputStream is, OutputStream os)
  7. uncompress(InputStream inputStream)
  8. unpackZip(Context context, String s, InputStream is)
  9. readEntryAsWhatEverInternal( final ZipEntry entry, final InputStream unzippedInputStream)