解压缩zip文件

来源:互联网 发布:2017淘宝美国章鱼哥 编辑:程序博客网 时间:2024/05/22 12:23

每个人的电脑上面都会装有这么一个工具--文件解压缩工具,如Winrar,这些都是别人做好的了,但是你有么有想过有那么一天自己也做那么一个.

在java.util.zip包中提供了一些可以完成解压缩的类,可以压缩格式对流进行读写.它们都是继承自字节流类OutputStream和InputStream.其中GZIPOutputStream 和ZipOutputStream 可分别把数据压缩成GZIP格式和Zip格式.GZIPInputStream 和ZipInputStream 可以分别把压缩成GZIP格式或Zip的数据解压缩恢复原状.那么来看一个简单的 例子.

例1.把文本文件Hello.txt 文件压缩,然后再解压出来.具体的代码如下:

/* // 开发人员:唐欢 //功能:压缩和解压文件 //时间:2013-12-15 19:39:53 */ import java.io.*; import java.util.zip.*; public class GZIPTester { public static void main(String[] args) throws IOException { FileInputStream in=new FileInputStream("E:\\java\\io\\temp\\Hello.txt"); GZIPOutputStream out =new GZIPOutputStream(new FileOutputStream("E:\\java\\io\\temp\\test.gz")); System.out.println("Writing compressing file from E:/java/io/temp/Hello.txt to E:/java/io/temp/test.gz"); int c;; while((c=in.read())!=-1) { out.write(c);  //写压缩文件 } in.close();  //关闭流 out.close(); //关闭流 System.out.println("Reading file form  E:/java/io/temp//test.gz to monitor");  //通过InputStreamReader将字节流转化为字符流 BufferedReader in2=new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("E:/java/io/temp//test.gz")))); String s; while ((s=in2.readLine())!=null )  //读每一行并显示 { System.out.println(s); } in2.close(); System.out.println("Writing decompression to E:\\java\\io\\temp\\newHello1.txt"); GZIPInputStream in3=new GZIPInputStream(new FileInputStream("E:\\java\\io\\temp\\test.gz")); FileOutputStream out2=new FileOutputStream("E:\\java\\io\\temp\\newHello1.txt"); while((c=in3.read())!=-1)  //写解压缩文件 { out2.write(c); } in3.close(); out2.close(); } }

运行结果:


运行结果是首先生成压缩文件test.gz,再读取其中的内容,显示结果和Hello.txt中的内容完全一样的.程序生成的解压缩文件"newHello1.txt"和"Hello.txt"中的内容是一样的.


例2.解压缩ZIP文件,并恢复期原来路径

/* // 开发人员:唐欢 //功能:解压缩zip文件,并恢复其原来路径 //时间:2013-12-15 20:09:04 */ import java.util.*; import java.util.zip.*; import java.lang.*; import java.io.*; public class UnZipTester { public static void main(String[] args) { String zipFile=args[0]; //第一个参数为zip文件名 String unZipPath=args[1]+"\\";  //第二个参数为指定解压缩文件 Unzip myZip=new Unzip(zipFile,unZipPath); //创建一个Unzip 类的实例 myZip.doUnZip();  //调用Unzip类构造函数外唯一公有方法,完成解压缩  } } class Unzip { byte doc[]=null; //存储解压缩数据的缓冲字节数组 String Filename=null;  //压缩文件名字符串 String UnZipPath=null;  //解压缩路径字符串  //指定压缩文件名和解压缩路径的构造函数 public Unzip(String filename,String unZipPath) { this.Filename=filename; this.UnZipPath=unZipPath; this.setUnZipPath(this.UnZipPath); }  //只指定压缩文件名的构造函数 public Unzip(String filename) { this.Filename=new String (filename); this.UnZipPath=null; this.setUnZipPath(this.UnZipPath); }  private void setUnZipPath(String unZipPath) { //确保路径名后有"/" if(unZipPath.endsWith("\\")) {  this.UnZipPath=new String(unZipPath); } else { this.UnZipPath=new String(unZipPath+"\\");  } }   //从zip文件中读取数据,并将解压缩文件保存到指定路径下 public void doUnZip() {  try  {  ZipInputStream zipis=new ZipInputStream(new FileInputStream(Filename));  ZipEntry fEntry=null;  while((fEntry=zipis.getNextEntry())!=null)  //直到压缩文件最后一个入口  {  if(fEntry.isDirectory())  //是路径则创建路径  {  checkFilePath(UnZipPath +fEntry.getName());  }  else  //是路径则创建路径  {  String fname=new String (UnZipPath+fEntry.getName());  //确定解压缩地址  try  {  //从压缩文件中读取解压缩数据存入字节数组中,并写入解压缩地址中  FileOutputStream out=new FileOutputStream(fname);  doc=new byte[512];  int n;  while((n=zipis.read(doc,0,512))!=-1)  {  out.write(doc,0,n);  }  out.close();  out=null;  doc=null;  }  catch(Exception ex)  {  }  }  }  zipis.close();  //关闭输入流 } catch(IOException ioe) { System.out.println(ioe); }}//检查路径是否存在,若不存在则创建private void checkFilePath(String dirName) throws IOException{ File dir=new File(dirName); if(!dir.exists()) {  dir.mkdirs();}}}
从例子中可以知道,Unzip类用来将指定zip文件解压到指定路径在主类UnzipTester的main函数中,可以从命令行输入zip文件名和解压缩路径.


从这两个例子中,我们可以看出,java都已经把这些解压缩类封装好了,在我们需要的时候,直接调用就可以了.这体现了java全心全意为人民服务的精神.

0 0