19
2019
06

java 解压zip文件操作

java 解压zip文件操作
实现类demo:

package com.info.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


public class TestDemo {
/**
* main
* 解压文件
*/
public static void main(String[] args) throws IOException {
    File zipFile = new File("f:/test.zip");//要解压的zip文件
    String path = "f:/zipfile/";//解压到指定位置
    unZipFiles(zipFile, path);//解压操作
    ScannerOperation();//解压后操作
  }
public static void unZipFiles(String zipPath,String descDir)throws IOException {
    unZipFiles(new File(zipPath), descDir);
}
  /**
   * 解压文件到指定目录
   */
  @SuppressWarnings("rawtypes")
  public static void unZipFiles(File zipFile,String descDir)throws IOException {
    File pathFile = new File(descDir);
    if(!pathFile.exists()){
      pathFile.mkdirs();
    }
    //解决zip文件中有中文目录或者中文文件
    ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
    for(Enumeration entries = zip.entries(); entries.hasMoreElements();) {
      ZipEntry entry = (ZipEntry)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;
      }
      //输出文件路径信息
      System.out.println(outPath);
      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();
    }
    System.out.println("******************解压完毕********************");
  }
  
  // 控制台操作 解压后可再删除
  public static void ScannerOperation(){
    Scanner sc = new Scanner(System.in);
    System.out.println("操作输入:1 退出,2 删除");
    String code = sc.nextLine();  //读取字符串型输入
    while(true) {
    if(code.equals("1")) {
    System.out.println("已退出");
    break;
    }else if(code.equals("2")) {
    deleteFile();
    System.out.println("已删除");
    break;
    }else {
    System.out.println("操作输入:1 退出,2 删除");
    code = sc.nextLine();  //读取字符串型输入
    }
    }
  }
  
  public static void deleteFile() {
  //指定删除路径
  System.out.println("删除状态:"+delAllFile("f:/zipfile/"));
  }
//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
   public static boolean delAllFile(String path) {
       boolean flag = false;
       File file = new File(path);
       if (!file.exists()) {
         return flag;
       }
       if (!file.isDirectory()) {
         return flag;
       }
       String[] tempList = file.list();
       File temp = null;
       for (int i = 0; i < tempList.length; i++) {
          if (path.endsWith(File.separator)) {
             temp = new File(path + tempList[i]);
          } else {
              temp = new File(path + File.separator + tempList[i]);
          }
          if (temp.isFile()) {
             temp.delete();
          }
          if (temp.isDirectory()) {
             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
             delFolder(path + "/" + tempList[i]);//再删除空文件夹
             flag = true;
          }
       }
       return flag;
     }
   
//删除文件夹
//param folderPath 文件夹完整绝对路径
public static void delFolder(String folderPath) {
      try {
         delAllFile(folderPath); //删除完里面所有内容
         String filePath = folderPath;
         filePath = filePath.toString();
         java.io.File myFilePath = new java.io.File(filePath);
         myFilePath.delete(); //删除空文件夹
      } catch (Exception e) {
        e.printStackTrace(); 
      }
}
}

测试结果 输出信息:
f:/zipfile/test/beijing.jpeg
f:/zipfile/test/wjj.jpg
f:/zipfile/test/wjj2.jpg
******************解压完毕********************
操作输入:1 退出,2 删除
1
已退出
or
操作输入:1 退出,2 删除
2
删除状态:true
已删除

注:删除会把指定文件夹 下的 所有文件及文件删除


« 上一篇 下一篇 »