java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件,用到ant.jar解决汉字乱码

废了几个小时时间整java解压缩zip这玩意,总算写出来了,呵呵,希望对看到朋友有用!

用到了ant.jar来解决压缩中的中文乱码问题。

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盘正则表达式文件夹下的所有文件压缩到Estu目录下,压缩后的文件名保存为 正则表达式.zip

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

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

       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。需注意的是,旧式的文件系统不支持大于 4 GB 的文件,此类的文件你必须使用 NTFS 磁盘格式才能正常工作。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值