Android Zip File Create compress(File file)

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

Description

compress

Declaration

public static void compress(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.GZIPOutputStream;

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

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

        /*w  w w.  j a v a 2s .  com*/
        compress(bais, baos);

        byte[] output = 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 output;
    }

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

    public static void compress(File file, boolean delete) throws Exception {
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);

        compress(fis, fos);

        fis.close();
        fos.flush();
        fos.close();

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

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

        GZIPOutputStream gos = null;
        try {
            gos = new GZIPOutputStream(os);
            int count;
            byte data[] = new byte[BUFFER];
            while ((count = is.read(data, 0, BUFFER)) != -1) {
                gos.write(data, 0, count);
            }

            gos.finish();

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

    }

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

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

Related

  1. zipFile(Collection resFileLIst, File zipFile, String comment)
  2. zipIt(File zipFile, ArrayList files, Context context)
  3. ZipFiles(File zip, File... srcFiles)
  4. createZip(String[] files, String zipFile)
  5. createZip(String[] files, String zipFile)
  6. compress(File file)
  7. compress(File file, boolean delete)
  8. compress(File file, boolean delete)
  9. compressToFile(File dest, String str)