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; } }
文件压缩之压缩包工具类
最新推荐文章于 2023-06-29 09:58:48 发布