⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unzip.java

📁 java写的zip/unzip
💻 JAVA
字号:
//这是一个用于进行ZIP文件解压的程序
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

public class Unzip{
   private ZipFile archive;
   private String zipName;
   private Enumeration zipFiles;
   public Unzip(String ZipName){
      try{
         zipName=ZipName;
         archive=new ZipFile(ZipName);
         zipFiles=archive.entries();
      }catch(Exception e){
      	 System.out.println(e);
      }

   }
   public String getName(){
      return zipName;
   }

   public int unCompress(){
      //建立一个用于读取数据的缓冲区
      byte[] buffer = new byte[16384];
      try{
          for (; zipFiles.hasMoreElements(); ){
      	     //得到下一个条目
             ZipEntry entry = (ZipEntry) zipFiles.nextElement();
             String filename = entry.getName();
             filename = filename.replace('/', File.separatorChar);

             File destFile = new File(filename);
             if(entry.isDirectory()){
                destFile.mkdirs();
                continue;
             }

             if (destFile.exists()){
                 System.out.println("..skipping "+filename+" (overwrite not implemented).");
             }
             else{
                 System.out.println("..writing "+filename);

                 //建立目的路径
                 String parent = destFile.getParent();

                 if(parent!=null){
                     File parentFile = new File(parent);
                     if (!parentFile.exists())
                         // create the chain of subdirs to the file
                         parentFile.mkdirs();

                 }
                 //建立一个从ZIP文件中读取特定条目数据的输入流
                 InputStream in = archive.getInputStream(entry);

                 //建立一个目标文件的输出流
                 OutputStream out = new FileOutputStream(filename);

                 //依次进行数据的读写,直到读到输入流的结尾为止
                 int count;
                 while((count=in.read(buffer)) != -1)
                     out.write(buffer, 0, count);

                 in.close();
                 out.close();
             }
          }
       }catch(Exception e){
       	   System.out.println(e);
       	   return 1;
       }
       return 0;
   }


   public static void main( String[] args ){
      Unzip unzip;
      if (args.length != 1){
          System.out.println("usage: Unzip file.zip");
          return;
      }

      if (!(new File(args[0])).exists()){
          System.out.println("can't open " + args[0]);
          return;
      }
      try{
         unzip=new Unzip(args[0]);
      }catch(Exception e){
      	 System.out.println(e);
      	 return;
      }

      unzip.unCompress();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -