使用Java压缩文件及目录

来源:互联网 发布:淘宝退货诈骗 编辑:程序博客网 时间:2024/05/29 14:52

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();

        }

    }

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 无法解析服务器的dns地址怎么办 笔记本dns辅服务器未响应怎么办 原营业执照注销法人没在怎么办? 加盟骗局公安局工商局不管怎么办 发现老婆衣柜里有避孕套怎么办 2岁宝宝爱舔东西怎么办 痔疮手术后痛疼怎么办 长了内痔疮该怎么办 淘宝卖家遇到恶意买家怎么办 淘宝遇到恶意买家拍单怎么办 淘宝遇到买家恶意搞坏商品怎么办 木油味道很重怎么办 床垫外面布坏了怎么办 淘宝店铺动态评分低怎么办 实创倒闭返款怎么办 qq红包忘记支付密码怎么办 qq红包支付密码忘了怎么办 个人卖东西人家要发票怎么办 打了欠条找不到人怎么办 公章掉进水里了怎么办 手游吃鸡限制登入15分钟怎么办 宝宝的小鸡淹了怎么办 宝宝小鸡底下淹了怎么办 4岁宝宝拉稀水怎么办 2岁半宝宝对眼怎么办 孩子在幼儿园中午不睡觉怎么办 20天婴儿拉稀水怎么办 3个月婴儿拉水怎么办 两岁宝宝拉水怎么办 1岁半宝宝拉水怎么办 5周孩子小鸡长脂肪粒怎么办 小孩丁丁长歪了怎么办 2个月的婴儿咳嗽怎么办 2个月的宝宝干咳怎么办 2月宝宝感冒咳嗽怎么办 产妇肚子上长小疙瘩很痒怎么办 泰迪生下小狗不吃奶怎么办? 刚生的狗不吃奶怎么办 刚出生的小牛不吃奶怎么办 吃奶的狗崽拉稀怎么办 新生儿吃奶老是呛到怎么办