小菜毛毛技术分享

与大家共同成长

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks

package unzip;

 

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

 

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import org.apache.tools.zip.ZipOutputStream;

 

/**

  * 功能: 1 、实现把指定文件夹下的所有文件压缩为指定文件夹下指定 zip 文件 2 、实现把指定文件夹下的 zip 文件解压到指定目录下

  *

  * @author ffshi

  *

  */

public class ZipUtils {

 

    public static void main(String[] args) {

 

       // 把 E 盘正则表达式文件夹下的所有文件压缩到 E 盘 stu 目录下,压缩后的文件名保存为 正则表达式 .zip

       // zip ("E:\\ 正则表达式 ", "E:\\stu \\ 正则表达式 .zip ");

       // 把 E 盘 stu 目录下的正则表达式 .zip 压缩文件内的所有文件解压到 E 盘 stu 目录下面

       unZip ( "E:\\stu\\ 正则表达式 .zip" , "E:\\stu" );

 

    }

 

    /**

      * 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件 create date:2009- 6- 9

      * author:Administrator

      *

      * @param sourceDir

      *             E:\\ 我的备份

      * @param zipFile

      *             格式: E:\\stu \\zipFile.zip 注意:加入 zipFile 我们传入的字符串值是

      *             : "E:\\stu \\" 或者 "E:\\stu "

      *             如果 E 盘已经存在 stu 这个文件夹的话,那么就会出现 java.io.FileNotFoundException: E:\stu

      *             ( 拒绝访问。 ) 这个异常,所以要注意正确传参调用本函数哦

      *

      */

    public static void zip(String sourceDir, String zipFile) {

       OutputStream os;

       try {

           os = new FileOutputStream(zipFile);

           BufferedOutputStream bos = new BufferedOutputStream(os);

           ZipOutputStream zos = new ZipOutputStream(bos);

 

           File file = new File(sourceDir);

 

           String basePath = null ;

           if (file.isDirectory()) {

              basePath = file.getPath();

           } else {

              basePath = file.getParent();

           }

 

           zipFile (file, basePath, zos);

 

           zos.closeEntry();

           zos.close();

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

 

    }

 

    /**

      *

      * create date:2009- 6- 9 author:Administrator

      *

      * @param source

      * @param basePath

      * @param zos

      * @throws IOException

      */

    private static void zipFile(File source, String basePath,

           ZipOutputStream zos) {

       File[] files = new File[0];

 

       if (source.isDirectory()) {

           files = source.listFiles();

       } else {

           files = new File[1];

           files[0] = source;

       }

 

       String pathName;

       byte [] buf = new byte [1024];

       int length = 0;

       try {

           for (File file : files) {

              if (file.isDirectory()) {

                  pathName = file.getPath().substring(basePath.length() + 1)

                         + "/" ;

                  zos.putNextEntry( new ZipEntry(pathName));

                  zipFile (file, basePath, zos);

              } else {

                  pathName = file.getPath().substring(basePath.length() + 1);

                  InputStream is = new FileInputStream(file);

                  BufferedInputStream bis = new BufferedInputStream(is);

                  zos.putNextEntry( new ZipEntry(pathName));

                  while ((length = bis.read(buf)) > 0) {

                     zos.write(buf, 0, length);

                  }

                  is.close();

              }

           }

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

 

    }

 

    /**

      * 解压 zip 文件,注意不能解压 rar 文件哦,只能解压 zip 文件 解压 rar 文件 会出现 java.io.IOException: Negative

      * seek offset 异常 create date:2009- 6- 9 author:Administrator

      *

      * @param zipfile

      *             zip 文件,注意要是正宗的 zip 文件哦,不能是把 rar 的直接改为 zip 这样会出现 java.io.IOException:

      *             Negative seek offset 异常

      * @param destDir

      * @throws IOException

      */

    public static void unZip(String zipfile, String destDir) {

 

       destDir = destDir.endsWith( "\\" ) ? destDir : destDir + "\\" ;

       byte b[] = new byte [1024];

       int length;

 

       ZipFile zipFile;

       try {

           zipFile = new ZipFile( new File(zipfile));

           Enumeration enumeration = zipFile.getEntries();

           ZipEntry zipEntry = null ;

 

           while (enumeration.hasMoreElements()) {

              zipEntry = (ZipEntry) enumeration.nextElement();

              File loadFile = new File(destDir + zipEntry.getName());

 

              if (zipEntry.isDirectory()) {

                  // 这段都可以不要,因为每次都貌似从最底层开始遍历的

                  loadFile.mkdirs();

              } else {

                  if (!loadFile.getParentFile().exists())

                     loadFile.getParentFile().mkdirs();

 

                  OutputStream outputStream = new FileOutputStream(loadFile);

                  InputStream inputStream = zipFile.getInputStream(zipEntry);

 

                  while ((length = inputStream.read(b)) > 0)

                     outputStream.write(b, 0, length);

 

              }

           }

           System. out .println( " 文件解压成功 " );

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

 

    }

 

}

 


也顺便网上查了下rar格式与zip格式有什么区别:


区别一、zip的安装比较大,并仅仅有英文版+汉化包
rar有官方的简体中文版,并且安装很小,不足一兆
区别二、winrar的压缩率较高,而zip的压缩率更低
区别三、zip支持的格式很多,但已经较老,不大流行
rar支持格式也很多,并且还是流行的
区别四、zip仅仅能够压缩成zip格式,不能解压rar格式;rar不仅有自己的格式,还可以压缩成zip格式并解压zip格式
区别五、zip的界面没有rar漂亮
区别六、winrar支持分卷压缩,zip不支持
区别七、国外很多都采用zip,因为它是免费的,rar不是免费的,在国内很流行是由于有盗版的存在;zip不能兼容rar,是因为这样必须付出一笔费用


WinRAR 可以创建两种不同的压缩文件格式: RAR 和 ZIP
ZIP 压缩文件

在 ZIP 文件的最大优点就是普及率。比如说,大部分在 Internet 的压缩文件都是 ZIP 压缩文件,所以如果你要传送压缩文件给某一个人,但你无法确定你的收件人是否有 WinRAR 来解压压缩文件的内容时,使用 ZIP 格式是个好推荐。要不然你也可以发送 自解压文件。此类的压缩文件稍微大了一点点,但不需要任何的外部程序便可以解压。

另一个 ZIP 的优点便是速度。 ZIP 压缩文件通常在创建时会比 RAR 快一些。

RAR 压缩文件

RAR 格式比 ZIP 更能够提供较好的压缩率,特别是在 固实模式 时。另外一个 RAR 的重要功能是支持 多卷 压缩文件。它们比起 ZIP 的“跨磁盘”压缩文件更加便利和简易。 WinRAR 不支持 ZIP 的磁盘拆分,如果你要创建分卷压缩文件,请使用 RAR 的分卷压缩来代替。

RAR 格式也有一些在 ZIP 中所缺乏的重要功能,例如 恢复记录,它允许物理受损数据的恢复,还能 锁定 重要的压缩文件,以防止它们被别人意外地更改。

RAR 格式可以管理的文件大小几乎是无限制的 (最大到 8,589,934,591 GB) ,而在 ZIP 压缩文件的单个文件的最大值为 4 GB。需注意的是,旧式的文

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xumiaohua/archive/2009/06/25/4297100.aspx


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xumiaohua/archive/2009/06/25/4297100.aspx

posted on 2010-01-18 16:42 小菜毛毛 阅读(4786) 评论(0)  编辑  收藏 所属分类: J2EE相关技术与框架

只有注册用户登录后才能发表评论。


网站导航: