JAVA文件压缩

xiaoxiao2021-02-28  144

最近工作中用到了文件压缩的功能,整理了一下。文件压缩的实现,可用JDK自带的 java.util.zip.功能,但是压缩时中文会有乱码,用org.apache.tools.zip.下的jar包可解决乱码问题,也可以修改JDK中的jar包,来决绝乱码问题。

文件压缩和解压代码:

package cn.lsh.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import org.apache.tools.ant.taskdefs.Mkdir; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; public class FileUtil { //文件压缩 public static void zipFiles(String zipFile,String path, File... srcFiles) throws IOException{ ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); FileUtil.zipFiles(zos, path, srcFiles); zos.close(); System.out.println("*********压缩完毕***********"); } public static void zipFiles(ZipOutputStream zos,String path,File... srcFiles){ path = path.replaceAll("\\*", "/"); if(!path.endsWith("/")){ path +="/"; } byte[] buf = new byte[1024]; try { for(int i = 0; i < srcFiles.length; i++){ if(srcFiles[i].isDirectory()){ File[] files = srcFiles[i].listFiles(); String srcPath =srcFiles[i].getName(); srcPath = srcPath.replaceAll("\\*", "/"); if(srcPath.endsWith("/")){ srcPath +="/"; } zos.putNextEntry(new ZipEntry(srcPath+path)); zipFiles(zos, srcPath+path, files); }else{ FileInputStream fis = new FileInputStream(srcFiles[i]); System.out.println(path+srcFiles[i].getName()); zos.putNextEntry(new ZipEntry(path+srcFiles[i].getName())); int len; while((len = fis.read(buf)) > 0){ zos.write(buf,0,len); } zos.closeEntry(); fis.close(); } } } catch (IOException e) { e.printStackTrace(); } } /** * 解压到指定目录 * @param args * @throws IOException */ public static void unZipFiles(String zipPath,String descDir) throws IOException{ //可以写一些校验 unZipFiles(new File(zipPath),descDir); } /** * 解压到指定目录 * @param args * @throws IOException */ private static void unZipFiles(File file, String descDir) { File pathFile = new File(descDir); if(!pathFile.exists()){ pathFile.mkdir(); } try { ZipFile zipFile = new ZipFile(file); for(Enumeration entries = zipFile.getEntries();entries.hasMoreElements();){ ZipEntry zipEntry =(ZipEntry) entries.nextElement(); String zipEmtryName = zipEntry.getName(); InputStream ins = zipFile.getInputStream(zipEntry); String outPath = (descDir+zipEmtryName).replaceAll("\\*", "/"); //判断路径是否存在 File dirFile = new File(outPath.substring(0, outPath.lastIndexOf("/"))); if(!dirFile.exists()){ dirFile.mkdir(); } //判断文件路径是否文件夹,若已上传,不需要解压 if(new File(outPath).isDirectory()){ continue; } System.out.println(outPath); OutputStream outs = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while((len=ins.read(buf1))>0){ outs.write(buf1,0,len); } ins.close(); outs.close(); } } catch (IOException e) { e.printStackTrace(); } System.out.println("****************解压完毕**************"); } //test public static void main(String[] args) throws IOException{ /*File[] files =new File[]{ new File("D:/Test/9527.xlsx"), new File("D:/Test/罗顺侯.docx"), new File("D:/Test/京北方.doc") };*/ /*String path = "D:/Test"; File[] files = new File(path).listFiles(); String zipFile ="D:/Test/压缩.zip"; zipFiles(zipFile, "abc", files);*/ //解压测试 File zipFile = new File("D:/Test/压缩.zip"); String descDir = "D:/Test/zipfile/"; unZipFiles(zipFile, descDir); } }
转载请注明原文地址: https://www.6miu.com/read-50443.html

最新回复(0)