IO流之ZipInputStream和ZipOutputStream的认识及使用

折月煮酒 提交于 2019-12-05 17:41:26

转载https://blog.csdn.net/weixin_39723544/article/details/80611810

工具类

import java.io.*;import java.util.zip.*;public class ZipUtils {    /**     * Default buff byte size     *     */    private static final int DEFAULT_BUFF_SIZE = 1024;    /**     * Default basedir value     *     */    private static final boolean DEFAULT_DIR = false;    public static void decompress(String srcPath) throws Exception {        decompress(new File(srcPath));    }    public static void decompress(File srcFile) throws Exception {        File baseFile = srcFile.getParentFile();        decompress(srcFile, baseFile);    }    public static void decompress(String srcPath, String destPath) throws Exception {        decompress(new File(srcPath), new File(destPath));    }    public static void decompress(File srcFile, File destFile) throws Exception {        CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());        ZipInputStream zis = new ZipInputStream(cis);        doDecompress(destFile, zis);        zis.close();    }    private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {        ZipEntry zipEntry = null;        while ((zipEntry = zis.getNextEntry()) != null) {            String dir = destFile.getPath() + File.separator + zipEntry.getName();            File dirFile = new File(dir);            fileProber(dirFile);            if (zipEntry.isDirectory()) {                dirFile.mkdirs();            } else {                doDecompressFile(dirFile, zis);            }            zis.closeEntry();        }    }    private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));        int len;        byte[] buff = new byte[DEFAULT_BUFF_SIZE];        while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {            bos.write(buff, 0, len);        }        bos.close();    }    /**     * 文件探测     *     * When the parent file not exist.Create it.     *     * @param dirFile     * @throws Exception     */    public static void fileProber(File dirFile) throws Exception {        File parentFile = dirFile.getParentFile();        if (!parentFile.exists()) {            fileProber(parentFile);            parentFile.mkdirs();        }    }    public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {        compress(new File(srcPath), new File(destPath), dirFlag);    }    public static void compress(String srcPath, String destPath) throws Exception {        compress(new File(srcPath), new File(destPath), DEFAULT_DIR);    }    public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {        compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);    }    public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {        if (srcFile.isDirectory()) {            if (dirFlag) {                doCompress(zos, srcFile, srcFile.getName() + File.separator);            } else {                doCompress(zos, srcFile, "");            }        } else {            doCompress(zos, srcFile, "");        }        zos.close();    }    public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {        if (file.isDirectory()) {            File[] files = file.listFiles();            for (int i = 0; i < files.length; i++) {                doCompress(zos, files[i], baseDir);            }        } else {            byte[] buff = new byte[DEFAULT_BUFF_SIZE];            InputStream in = new FileInputStream(file);            zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));            int len;            while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {                zos.write(buff, 0, len);            }            in.close();        }    }}}压缩
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class condense {    private static final boolean DEFAULT_DIR = true;    private static final int DEFAULT_BUFF_SIZE = 1024;    // 压缩入口1    public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {        compress(new File(srcPath), new File(destPath), dirFlag);    }    // 压缩入口2    public static void compress(String srcPath, String destPath) throws Exception {        compress(new File(srcPath), new File(destPath), DEFAULT_DIR);    }    // 压缩入口3    public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {        compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);    }    public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {        // 需要解压的压缩文件对象        // 压缩输出流        // 是否在压缩文件时创建一个父文件夹后再压缩        if (srcFile.isDirectory()) {            if (dirFlag) {                doCompress(zos, srcFile, srcFile.getName() + File.separator);            } else {                doCompress(zos, srcFile, "");            }        } else {            doCompress(zos, srcFile, "");        }        zos.close();    }    public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {        if (file.isDirectory()) {            // 递归循环,只压缩其中所有文件            File[] files = file.listFiles();            for (int i = 0; i < files.length; i++) {                doCompress(zos, files[i], baseDir);            }        } else {            // 进行文件压缩的操作            byte[] buff = new byte[DEFAULT_BUFF_SIZE];            InputStream in = new FileInputStream(file);            zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));            int len;            while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {                zos.write(buff, 0, len);            }            in.close();        }    }}
解压
import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.CRC32;import java.util.zip.CheckedInputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;public class decompress {    /**     * Default buff byte size     */    private static final int DEFAULT_BUFF_SIZE = 1024;    // 程序入口1    public static void decompress(String srcPath) throws Exception {        decompress(new File(srcPath));    }    // 程序入口2    public static void decompress(File srcFile) throws Exception {        File baseFile = srcFile.getParentFile();        decompress(srcFile, baseFile);    }    // 程序入口3    public static void decompress(String srcPath, String destPath) throws Exception {        decompress(new File(srcPath), new File(destPath));    }    // 程序基本入口    public static void decompress(File srcFile, File destFile) throws Exception {        CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());        ZipInputStream zis = new ZipInputStream(cis);        // 解压操作        doDecompress(destFile, zis);        zis.close();    }    private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {        ZipEntry zipEntry = null;        while ((zipEntry = zis.getNextEntry()) != null) {            String dir = destFile.getPath() + File.separator + zipEntry.getName();            File dirFile = new File(dir);            // 如果父文件夹不存在,则递归创建其父文件夹            fileProbe(dirFile);            if (zipEntry.isDirectory()) {                // 如果zipEntry是目录,则创建目录                dirFile.mkdirs();            } else {                // 解压压缩文件的其中具体的一个zipEntry对象                doDecompressFile(dirFile, zis);            }            zis.closeEntry();        }    }    // 一般意义上的文件复制操作    private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));        int len;        byte[] buff = new byte[DEFAULT_BUFF_SIZE];        while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {            bos.write(buff, 0, len);        }        bos.close();    }    /**     * 文件探测     *     * When the parent file not exist.Create it.     *     * @param dirFile     * @throws Exception     */    public static void fileProbe(File dirFile) throws Exception {        File parentFile = dirFile.getParentFile();        if (!parentFile.exists()) {            fileProbe(parentFile);            parentFile.mkdirs();        }    }}
 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!