Android Zip File Create zipFiles(String compressName, File[] files)

Here you can find the source of zipFiles(String compressName, File[] files)

Description

compress some files to a zip file

Parameter

Parameter Description
compressName target zip file name
files files to be compressed

Return

compressed zip file

Declaration

public static File zipFiles(String compressName, File[] files) 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main{
    /**/*from ww w  . j  a  va 2 s .  c o m*/
     * compress some files to a zip file
     * @param compressName target zip file name
     * @param files files to be compressed
     * @return compressed zip file
     */
    public static File zipFiles(String compressName, File[] files) {
        File targetFile = new File(System.getProperty("java.io.tmpdir")
                + compressName + ".zip");
        try {
            byte[] buf = new byte[1024];
            int len;
            FileOutputStream fos = new FileOutputStream(targetFile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            ZipOutputStream zos = new ZipOutputStream(bos);
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                ZipEntry ze = new ZipEntry(file.getName());
                zos.putNextEntry(ze);
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                while ((len = bis.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                    zos.flush();
                }
                bis.close();
            }
            zos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        return targetFile;
    }
    /**
     * Read data from file
     * @param fileName file's name
     * @return file's data
     */
    public static byte[] read(String fileName) {
        byte[] bytes = null;
        try {
            if (StringUtil.isEmpty(fileName)) {
                return null;
            }
            InputStream in = new BufferedInputStream(new FileInputStream(
                    fileName));
            bytes = new byte[in.available()];
            in.read(bytes);
            in.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return bytes;
    }
    /**
     * Read data from file
     * @param file file to be read
     * @return file's data
     */
    public static byte[] read(File file) {
        byte[] bytes = null;
        try {
            InputStream ins = new BufferedInputStream(new FileInputStream(
                    file));
            bytes = new byte[ins.available()];
            ins.read(bytes);
            ins.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return bytes;
    }
}

Related

  1. generateKMZ(String inputFileName, String outputFileName)
  2. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  3. zipFiles(Collection resFileList, File zipFile)
  4. zipFiles(Collection resFileList, File zipFile, String comment)
  5. zipFiles(Collection resFileList, File zipFile)