Zip文件解压

来源:互联网 发布:李斯璇 留学爆料 知乎 编辑:程序博客网 时间:2024/04/30 10:29

/*
 * 功能: 文件解压
 * 用法: java ExtractZipFile zipfile DestinationPath
 * 注意: 不能建立子目录
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ExtractZipFile {
 public int scan(String filename) {
  ZipEntry entry = null;
  int filecount = 0;

  try {
   ZipInputStream zin = new ZipInputStream(new FileInputStream(
     filename));
   System.out.println("开始扫描 " + filename + ":");
   System.out.println("-------------------------");
   while ((entry = zin.getNextEntry()) != null) {
    if (entry.isDirectory() || entry.getName().equals("..//")) {
     continue;
    }
    System.out.println(entry.getName());
    filecount++;
    zin.closeEntry();
   }
   zin.close();
   return filecount;
  } catch (IOException e) {
   System.out.println(e);
   return -1;
  }
 }

 public static void main(String[] args) throws Exception {
  if (args.length != 2) {
   System.out.println("*******************************************");
   System.out.println("*    用法: java zip/jar 文件 目标路径         *");
   System.out.println("* Usage: java zip/jarFile DestinationPath *");
   System.out.println("*******************************************");
   System.exit(-1);
  }
  // First Scan the Zip File
  ScanZipFile scanzipfile = new ScanZipFile();
  if (scanzipfile.scan(args[0]) < 0) {
   System.out.println("Unzip Failed! Exiting now...");
   System.exit(-1);
  } else {
   System.out.println("*******************************************");
   System.out
     .println("* Enter Y or y to extract the files, Any other key to exit! *");
   System.out.println("*******************************************");

   String inputString = "";
   byte[] buffer = new byte[255];
   try {
    System.in.read(buffer, 0, 255);
    inputString = new String(buffer);

   } catch (IOException ex) {
    System.out.println(ex);
   }
   if (inputString.indexOf("Y") != -1
     || inputString.indexOf("y") != -1) {
    try {
     ZipInputStream zin = new ZipInputStream(
       new FileInputStream(args[0]));
     ZipEntry entry;
     System.out.println("Now Extracting the " + args[0] + ":");
     String filename;
     while ((entry = zin.getNextEntry()) != null) {
      if (entry.isDirectory()
        || entry.getName().equals("..//")) {
       continue;
      }
      System.out.println(entry.getName());
      filename = args[1] + "//" + entry.getName();
      byte[] buf = new byte[1];
      BufferedInputStream bin = new BufferedInputStream(zin);
      FileOutputStream tfos = new FileOutputStream(filename);
      BufferedOutputStream bout = new BufferedOutputStream(
        tfos);
      while (bin.read(buf, 0, 1) != -1) {
       bout.write(buf, 0, 1);
      }
      bout.close();
      zin.closeEntry();
     }
     zin.close();
     System.out
       .println("*******************************************");
     System.out
       .println("*    Extract Over                    ******");
     System.out
       .println("*******************************************");
    } catch (IOException e) {
     System.out.println(e);
    }
   } else {
    System.out
      .println("*******************************************");
    System.out
      .println("*    Extract Quit                    ******");
    System.out
      .println("*******************************************");

    System.exit(0);
   }
  }
 }
 /*
  * 这个程序接收2个参数, 前一个参数表示要解压的 zip 文件, 后一个参数表示将文件解压到那个指定 目录下.它首先建立一个 ScanZipFile
  * 对象, 然后执行这个对象上的 scan 方法, 将这个压缩文件 中的内容都显示到控制台上, 然后, 它试图从控制台得到一个指令,
  * 用于指明是否对该压缩文件进行解压, 输入 y 或 Y, 将会开始执行解压操作,否则, 将退出整个应用.
  */
}
 

原创粉丝点击