使用Java压缩文件及目录

1、压缩文件

 

            logger.info("Start to compress!");

            File file = new File("C:/1.csv");
            String zipFileName = "C:/test.zip";
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
            out.setMethod(ZipOutputStream.DEFLATED);
            out.putNextEntry(new ZipEntry("1.csv"));
            FileInputStream in = new FileInputStream(file);
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
            out.close();
            logger.info("End compressing!");

 

 

2、压缩目录

 public class TestZip {
     public static String SERPEROT = "/";
     public static int BUFFER = 2048;
   
     public static void main(String args[]) {
        zip("e:/hello/", "e:/hello.zip");
     }

 

     public static void zip(String srcFile, String descFile) {
        ZipOutputStream zos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            fos = new FileOutputStream(descFile);
            zos = new ZipOutputStream(fos);
            file = new File(srcFile);
            String folder = srcFile.substring(srcFile.lastIndexOf("/") + 1, srcFile.length());
            zip(zos, file, folder);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void zip(ZipOutputStream descFile, File srcFile, String srcfolder) {
        FileInputStream fis = null;
        System.out.println(srcFile.isDirectory());
        try {
            if (srcFile.isDirectory()) {
                File[] files = srcFile.listFiles();
                descFile.putNextEntry(new ZipEntry(srcfolder + "/")); // 是压缩包里面的路径.
                srcfolder = srcfolder.length() == 0 ? "" : srcfolder + "/";
                System.out.println(srcfolder);
                for (int i = 0; i < files.length; i++) {
                    zip(descFile, files[i], srcfolder + files[i].getName());
                }
            } else {
                descFile.putNextEntry(new ZipEntry(srcfolder));
                fis = new FileInputStream(srcFile);
                byte[] bytes = new byte[2048];
                int n = 0;
                while ((n = fis.read(bytes)) != -1) {
                    descFile.write(bytes, 0, n);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

 

3. 解压目录及文件

解压缩与压缩运作方式相反,原理大抵相同,由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码

    privatevoid unzipFile(InputStream in)throws IOException {

        finalint bufferSize = 1024;

        ZipInputStream zip = null;

        BufferedOutputStream bos = null;

        try {

            CheckedInputStream cis =new CheckedInputStream(in,new CRC32());

            zip = new ZipInputStream(cis);

            ZipEntry entry = null;

            while ((entry = zip.getNextEntry()) !=null) {

                StringBuilder entryPath =new StringBuilder(PathConstants.SALT_FILE_ROOT);

                 entryPath.append(entry.getName());

                File entryFile = new File(entryPath.toString());

                  fileProber(entryFile);

                if (entry.isDirectory()) {

                    entryFile.mkdir();

                } else {

                    bos = new BufferedOutputStream(new FileOutputStream(entryFile));

                    int count;

                    byte data[] =newbyte[bufferSize];

                    while ((count = zip.read(data, 0, bufferSize)) != -1) {

                      bos.write(data, 0, count);

                    }

                    bos.close();

                }

                zip.closeEntry();

            }

        } finally {

            if (zip !=null) {

                try {

                    zip.close();

                } catch (IOException e) {

                    logger.error(e.getMessage(), e);

                }

            }

            if (bos !=null) {

                try {

                    bos.close();

                } catch (IOException e) {

                    logger.error(e.getMessage(), e);

                }

            }

        }

    }

    privatevoid fileProber(File dirFile) {

        File parentFile = dirFile.getParentFile();

        if (!parentFile.exists()) {

            fileProber(parentFile);

            parentFile.mkdir();

        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值