文件压缩之压缩包工具类

package cn.dreamit.p2047.module_200730185526Po0wk3EgoVQLuKxFyNJ.utils;

import cn.dreamit.dreamweb.util.FileUtil;
import cn.dreamit.dreamweb.util.ValueUtil;
import cn.dreamit.p1000.util.SuperKit;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;
import java.util.Map;
import java.util.Properties;

public class CompressFileUtil {

    //生成压缩包
    public static boolean createZip(Map<String, File> filesMap, String zipPath) {
        ZipOutputStream zout = null ;
        FileOutputStream out = null;
        InputStream in = null;

        boolean result = false;

        try {
            //创建zip文件夹
            File file=new File(zipPath);
            if(!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            //创建zip文件
            file.createNewFile();

            out = new FileOutputStream(file);
            zout = new ZipOutputStream(out);
//            zout.setEncoding("UTF-8") ;

            //zip文件添加压缩文件
            for(String bt : filesMap.keySet()){
                System.out.println("INFO [wuj]:bt " + bt);
                //被压缩文件路径
                String newFilePath = String.valueOf(filesMap.get(bt));
                File f = new File(newFilePath);
                zout.setEncoding("GBK") ;
//                zout.setEncoding(System.getProperty("sun.jnu.encoding"));
                ZipEntry entry = new ZipEntry(bt);
                System.out.println("INFO [wuj]:bt " + entry.getName());

                Properties pro=System.getProperties();
                System.out.println("INFO [wuj]:file.encoding = " + System.getProperty("file.encoding"));
                String osName=pro.getProperty("os.name");
                System.out.println("osName = " + osName);
                if("Linux".equals(osName)||"linux".equals(osName)){
                    if (file.isDirectory()) {
                        entry.setUnixMode(755);
                    }else {
                        entry.setUnixMode(644);
                    }
                }
                zout.putNextEntry(entry);
                System.out.println("INFO [wuj]:bt " + entry.getName());
                //是否加密
                String encryptMode = SuperKit.getStringFromDBConfig("SYS-ATTACH-0006");
                boolean unencrypted = ValueUtil.getInt(encryptMode) <= 1;



                //需解密
                if(!unencrypted && !bt.contains(".xml")){
                    byte[] content;
                    content = FileUtil.decryptFile(FileUtil.readFile(newFilePath));
                    zout.write(content);
                }else{
                    in = new FileInputStream(f);
                    int len;
                    byte[] buf = new byte[2048];
                    while ((len = in.read(buf)) != -1){
                        zout.write(buf, 0, len);
                    }
                }

                zout.closeEntry();
                if(in!=null) {
                    in.close();
                }
            }

            result = true;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(in!=null) {
                    in.close();
                }
                if(zout!=null) {
                    zout.close();
                }
                if(out!=null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
            }
        }

        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值