溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-04-14 13:49:15 來(lái)源:億速云 閱讀:125 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)java如何實(shí)現(xiàn)文件夾解壓和壓縮,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

效果

實(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);
  }
 }
 }
}

關(guān)于“java如何實(shí)現(xiàn)文件夾解壓和壓縮”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI