欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)文件夾解壓和壓縮

 更新時(shí)間:2020年03月19日 08:11:43   作者:Supper猿  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件夾解壓和壓縮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)文件夾解壓和壓縮的具體代碼,供大家參考,具體內(nèi)容如下

效果

實(shí)現(xiàn)多個(gè)文件以及文件夾的壓縮和解壓

代碼分析

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Main {
  public static void main(String[] args) throws IOException {
   //解決中文亂碼
   //壓縮 參數(shù)改成你自己的源文件路徑和壓縮后的文件路徑
 //yasuo("C:\\file\\", "C:\\file.zip");
 //解壓 參數(shù)改成你自己的源文件路徑和解壓后的文件路徑
 jieya("C:\\file.zip", "C:\\file\\");
  }

 public static void jieya(String zipPath, String path) throws IOException, FileNotFoundException {
 //創(chuàng)建解壓后的文件夾
 File pt=new File(path.substring(0,path.length()-1));
 if(!pt.exists()) {
  pt.mkdirs();
 }
 //try(resource)來(lái)保證InputStream正確關(guān)閉
 try(ZipInputStream zip=new ZipInputStream(new FileInputStream(zipPath))){
  //ZipEntry表示一個(gè)壓縮文件或目錄
  ZipEntry entry;
  while((entry=zip.getNextEntry())!=null) {
  String name=entry.getName();
  //壓縮文件
  if(!(entry.getName().contains(File.separator))) {
   FileOutputStream file= new FileOutputStream( path+ name);
   int n=0;
   while((n=zip.read())!=-1) {
   file.write(n);
   }
  }else {
  //目錄
   int index=name.lastIndexOf("\\");
   File file= new File(path+ name.substring(0,index));
   if(!file.exists()) {
   file.mkdirs();
   }
   //如果不是空目錄
   if(index!=name.length()-1) {
   FileOutputStream f= new FileOutputStream( path+ name);
   int n=0;
   while((n=zip.read())!=-1) {
    f.write(n);
   }
   }
  }
  }
  zip.closeEntry();
 }
 }

 public static void yasuo(String path, String zipPath) throws IOException, FileNotFoundException {
 File zp=new File(zipPath);
 if(!zp.exists()) {
  zp.createNewFile();
 }
 
 try(ZipOutputStream zip=new ZipOutputStream(new FileOutputStream(zp))) {
   File files= new File(path);
   File[] f=files.listFiles();
   for (File file : f) {
    zipAll(zip, file,file.getName());
  }
  
   }
 }

 public static void zipAll(ZipOutputStream zip, File files,String name) throws IOException, FileNotFoundException {
 if(files.isDirectory()) {
  File[] files2=files.listFiles();
  if(files2.length==0||files2==null) {
  zip.putNextEntry(new ZipEntry(name+File.separator));
  }else{
  for (File file2 : files2) {
   if(file2.isFile()) {
   zip.putNextEntry(new ZipEntry(name+File.separator+file2.getName()));
   int n;
   FileInputStream input=new FileInputStream(file2);
   while((n=input.read())!=-1) {
    zip.write(n);
   }
   }
   else {
   zipAll(zip,file2,name+File.separator+file2.getName());
   }
  }
  }
 }else {
  zip.putNextEntry(new ZipEntry(name));
  int n;
  FileInputStream input=new FileInputStream(files);
  while ((n=input.read())!=-1) {
  zip.write(n);
  }
 }
 }
}

小結(jié)

壓縮

ZipOutputStream可以把多份數(shù)據(jù)寫(xiě)入zip包;

解壓

ZipInputStream可以讀取zip格式的流;

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論