Android 文件压缩

1,726 阅读2分钟

文件压缩

public class ZipUtils {
    private static final int BUFFER_LEN = 8192;

    private ZipUtils() {
        throw new UnsupportedOperationException("error");
    }

    /**
     * 压缩文件(夹).
     *
     * @param srcFiles 源文件.
     * @param zipFile  目标文件.
     * @return {@code true}: 成功 <br>{@code false}: 失败
     * @throws IOException IO异常
     */
    public static boolean zipFiles(final Collection<File> srcFiles, final File zipFile)
            throws IOException {
        return zipFiles(srcFiles, zipFile, null);
    }

    /**
     * 压缩文件(夹).
     *
     * @param srcFiles 源文件.
     * @param zipFile  目标文件.
     * @param comment  描述.
     * @return {@code true}: 成功 <br>{@code false}: 失败
     * @throws IOException IO异常
     */
    public static boolean zipFiles(final Collection<File> srcFiles,
                                   final File zipFile,
                                   final String comment)
            throws IOException {
        if (srcFiles == null || zipFile == null) {
            return false;
        }
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
            for (File srcFile : srcFiles) {
                if (!zipFile(srcFile, "", zos, comment)) {
                    return false;
                }
            }
            return true;
        } finally {
            if (zos != null) {
                zos.finish();
                CloseUtils.closeIO(zos);
                Log.i("===========","压缩完成");
           }
        }
    }    /**
     * 压缩文件
     *
     * @param srcFile  源文件
     * @param rootPath 目标文件
     * @param zos      压缩输入流
     * @param comment  描述
     * @return {@code true}: 成功 <br>{@code false}: 失败
     * @throws IOException IO异常
     */
    private static boolean zipFile(final File srcFile,
                                   String rootPath,
                                   final ZipOutputStream zos,
                                   final String comment)
            throws IOException {
        rootPath = rootPath + (isSpace(rootPath) ? "" : File.separator) + srcFile.getName();
        if (srcFile.isDirectory()) {
            File[] fileList = srcFile.listFiles();
            if (fileList == null || fileList.length <= 0) {
                ZipEntry entry = new ZipEntry(rootPath + '/');
                entry.setComment(comment);
                // 设置压缩方式为存储方式需要添加的代码,如为默认压缩方式直接注释
                entry.setMethod(ZipEntry.STORED);
                entry.setCompressedSize(srcFile.length());
                entry.setSize(srcFile.length());
                entry.setCrc(getCrc(srcFile));
                //------END---------//
                zos.putNextEntry(entry);
                zos.closeEntry();
            } else {
                for (File file : fileList) {
                    if (!zipFile(file, rootPath, zos, comment)) {
                        return false;
                    }
                }
            }
        } else {
            InputStream is = null;
            try {
                is = new BufferedInputStream(new FileInputStream(srcFile));
                ZipEntry entry = new ZipEntry(rootPath);
                entry.setComment(comment);
                // 设置压缩方式为存储方式需要添加的代码,如为默认压缩方式直接注释
                entry.setMethod(ZipEntry.STORED);
                entry.setCompressedSize(srcFile.length());
                entry.setSize(srcFile.length());
                entry.setCrc(getCrc(srcFile));
                //------END---------//
                zos.putNextEntry(entry);
                byte buffer[] = new byte[BUFFER_LEN];
                int len;
                while ((len = is.read(buffer, 0, BUFFER_LEN)) != -1) {
                    zos.write(buffer, 0, len);
                }
                zos.closeEntry();
            } finally {
                CloseUtils.closeIO(is);
            }
        }
        return true;
    }


    public static long getCrc(File srcFile) throws IOException {
        CRC32 crc32 = new CRC32();
        crc32.update(getFileBytes(srcFile));
        return crc32.getValue();
    }

    /**
     * 获取文件的bytes<br>
     *
     * @param file 要读取的文件
     *             文件太大可能会出现 OOM
     * @return 读取文件得到的字节数组
     */
    private static byte[] getFileBytes(File file) throws IOException {
        byte[] buffer;
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(2048);
        byte[] b = new byte[2048];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        CloseUtils.closeIO(fis);
        CloseUtils.closeIO(bos);
        buffer = bos.toByteArray();
        return buffer;
    }

    private static boolean isSpace(final String s) {
        if (s == null) {
            return true;
        }
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
            return false;
            }
        }
        return true;
    }
}

解压代码待更新~