压缩zip文件和解压zip格式的文件
来源:互联网 发布:域名dns劫持检测 编辑:程序博客网 时间:2024/06/02 21:05
<span style="white-space:pre"></span>/****单文件压缩**/@Testpublic void zip() throws IOException{String path="D://compressssss//b.zip";File file=new File(path.substring(0, path.lastIndexOf("//")));//如果不存在这个文件夹就创建这个文件夹if(!file.exists()){file.mkdirs();}ZipArchiveOutputStream zipOut=new ZipArchiveOutputStream(new FileOutputStream(path));InputStream in=new FileInputStream("E://test.txt");ZipArchiveEntry entry=new ZipArchiveEntry("test.txt");zipOut.putArchiveEntry(entry);byte[] b=new byte[1024];int len=0;while((len=in.read(b))!=-1){zipOut.write(b,0,len);}zipOut.closeArchiveEntry();zipOut.close();}/** * 多文件压缩成zip包 * @throws IOException */@Testpublic void MulFileCompress() throws IOException{//压缩包的指定路径String path="D://zip//code.zip";File file2=new File(path.substring(0, path.lastIndexOf("//")));//如果不存在这个文件夹就创建这个文件夹if(!file2.exists()){file2.mkdirs();}ZipArchiveOutputStream zipOut=new ZipArchiveOutputStream(new FileOutputStream(path));//将要进行压缩的文档"E:\\工作文档"File file=new File("E:\\工作文档");System.out.println(file.getAbsolutePath());String filepath=file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\")+1);System.out.println(filepath);getFile(file,zipOut,filepath);zipOut.close();}private void getFile(File file,ZipArchiveOutputStream zipOut,String filepath) throws IOException{boolean isDirectory=file.isDirectory();//判断子文件是否是目录是的话进行递归,不是目录而是文件的话,就直接进行压缩if(isDirectory){File[] files=file.listFiles();for (File f : files) {getFile(f,zipOut,filepath);}}else{InputStream in=new FileInputStream(file.getAbsolutePath());if(file.getAbsolutePath()!="D://"){String path=file.getAbsolutePath().substring(file.getAbsolutePath().indexOf(filepath)+filepath.length());System.out.println(path);ZipArchiveEntry entry=new ZipArchiveEntry(path);zipOut.putArchiveEntry(entry);byte[] b=new byte[1024];int len=0;while((len=in.read(b))!=-1){zipOut.write(b,0,len);}zipOut.closeArchiveEntry();}}}/** * 解压zip文件 * @throws IOException */@Testpublic void Test() throws IOException{File file=new File("D://zip//code.zip");unCompress(file,"D://unzip//");}public void unCompress(File zipFile,String descDir)throws IOException{ File pathFile = new File(descDir); if(!pathFile.exists()){ pathFile.mkdirs(); } ZipFile zip = new ZipFile(zipFile); for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){ ZipArchiveEntry entry = (ZipArchiveEntry)entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); String outPath = (descDir+zipEntryName).replaceAll("\\*", "/"); //判断路径是否存在 File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if(!file.exists()){ file.mkdirs(); } //判断文件全路径是否为文件夹 if(new File(outPath).isDirectory()){ continue; } OutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while((len=in.read(buf1))>0){ out.write(buf1,0,len); } in.close(); out.close(); } }
0 0
- 压缩zip文件和解压zip格式的文件
- 文件压缩和解压zip
- 压缩和解压zip文件
- zip文件的压缩和解压
- 写着玩的zip解压和压缩zip文件
- 解压.zip格式的文件
- .zip文件解压、压缩
- C# 文件压缩与解压(ZIP格式)
- C# 文件压缩与解压(ZIP格式)
- C# 文件压缩与解压(ZIP格式)
- C# 文件压缩与解压(ZIP格式)
- Java 实现zip格式的文件压缩与解压
- java对zip格式的文件压缩解压工具类
- Java 解压和压缩密码zip文件
- Ionic.Zip.dll文件压缩和解压
- Ionic.Zip.dll文件压缩和解压
- linux 安装zip和压缩解压文件
- java解压和压缩zip文件
- 第4章 数据访问层
- python之sklearn
- boot loader能全部用C程序编写吗
- 手机建站的几大必备要素
- Java虚拟机(五):虚拟机类加载
- 压缩zip文件和解压zip格式的文件
- 12c trancate table cascade
- PAFA 框架报错java.io.FileNotFoundException: class path resource [log4j.properties] cannot be resolved to
- maven 分模块开发
- 使用shell实现对xml文件的指定字段进行修改,默认替换成当时的时间戳
- netfilter/iptables全攻略
- iOS 支付密文实现方案
- Windows环境下svn服务器的安装步骤
- unix之内存管理