压缩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