什么是使用java解压缩zip文件的最好方法 [英] What is the best way to extract a zip file using java

查看:127
本文介绍了什么是使用java解压缩zip文件的最好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个压缩文件。该文件也包含各种目录和文件。我想提取所有这些并保存在指定的路径。

I have a a zipped file. That file contains various directories and files also. I want to extract all those and save in a specified path.

那么如何编写一个java程序来解压缩文件。

So How to write a java program to extract the zipped file.

感谢
Sunil Kumar Sahoo

Thanks Sunil Kumar Sahoo

推荐答案

public static  void unzip(){
      try{
           BufferedOutputStream out = null;
           ZipInputStream  in = new ZipInputStream(ZipFileExtracter.class.getClassLoader().getResourceAsStream("com/artificialmachines/chitme/stamps/ChitMeData.zip"));
           ZipEntry entry;
           boolean isDirectory=false;
           while((entry = in.getNextEntry()) != null){
               int count;
               byte data[] = new byte[BUFFER];
               // write the files to the disk
               String entryName = entry.getName();
               File newFile = new File(new StringBuffer().append(System.getProperty("user.dir")).append(File.separator).append(entryName).toString());
               if(entryName.endsWith("/")){
                   isDirectory=true;
                   newFile.mkdir();
                   //System.out.println("This is directory "+newFile.exists()+"  IS DIr "+newFile.isDirectory()+"    path "+newFile.getPath());
               }else{
                   newFile.createNewFile();
               }
               if(!isDirectory){
                   out = new BufferedOutputStream(new FileOutputStream(newFile),BUFFER);
                    while ((count = in.read(data,0,BUFFER)) != -1){
                         out.write(data,0,count);
                    }
                    cleanUp(out);
               }
               isDirectory=false;
           }
           cleanUp(in);
      }
      catch(Exception e){
           e.printStackTrace();
           System.exit(0);
      }
   }

这篇关于什么是使用java解压缩zip文件的最好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆