转自:http://www.cdtarena.com/java.html
​Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。

我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。

ZipFile

java中的每一个压缩文件都是可以使用ZipFile来进行表示的。
[java] view plaincopyprint?
  1. File file = new File("F:/zippath.zip");
  2. ZipFile zipFile = new ZipFile(file);
  3. System.out.println("压缩文件的名称为:" + zipFile.getName());
压缩单个文件
[java] view plaincopyprint?
  1. /** 压缩单个文件*/
  2. public static void ZipFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath);
  5. File zipFile = new File(zippath);
  6. InputStream input = new FileInputStream(file);
  7. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
  8. zipOut.putNextEntry(new ZipEntry(file.getName()));
  9. int temp = 0;
  10. while((temp = input.read()) != -1){
  11. zipOut.write(temp);
  12. }
  13. input.close();
  14. zipOut.close();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
应用:
[java] view plaincopyprint?
  1. ZipFile("d:/hello.txt", "d:/hello.zip");
压缩多个文件(文件夹)
[java] view plaincopyprint?
  1. /** 一次性压缩多个文件,文件存放至一个文件夹中*/
  2. public static void ZipMultiFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath);// 要被压缩的文件夹
  5. File zipFile = new File(zippath);
  6. InputStream input = null;
  7. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
  8. if(file.isDirectory()){
  9. File[] files = file.listFiles();
  10. for(int i = 0; i < files.length; ++i){
  11. input = new FileInputStream(files[i]);
  12. zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
  13. int temp = 0;
  14. while((temp = input.read()) != -1){
  15. zipOut.write(temp);
  16. }
  17. input.close();
  18. }
  19. }
  20. zipOut.close();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
应用:成都java培训
[java] view plaincopyprint?
  1. ZipMultiFile("f:/uu", "f:/zippath.zip");
解压缩单个文件 
[java] view plaincopyprint?
  1. /**  解压缩(解压缩单个文件)*/  www.cdtarena.com
  2. public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
  3. try {
  4. File file = new File(zippath);//压缩文件路径和文件名
  5. File outFile = new File(outfilepath);//解压后路径和文件名
  6. ZipFile zipFile = new ZipFile(file);
  7. ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
  8. InputStream input = zipFile.getInputStream(entry);
  9. OutputStream output = new FileOutputStream(outFile);
  10. int temp = 0;
  11. while((temp = input.read()) != -1){
  12. output.write(temp);
  13. }
  14. input.close();
  15. output.close();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
应用:
[java] view plaincopyprint?
  1. ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
解压缩多个文件
ZipInputStream类:
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了。
 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类。
[java] view plaincopyprint?
  1. /**  解压缩(压缩文件中包含多个文件)可代替上面的方法使用。
  2. * ZipInputStream类
  3. * 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,
  4. * 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
  5. * */
  6. public static void ZipContraMultiFile(String zippath ,String outzippath){
  7. try {
  8. File file = new File(zippath);
  9. File outFile = null;
  10. ZipFile zipFile = new ZipFile(file);
  11. ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
  12. ZipEntry entry = null;
  13. InputStream input = null;
  14. OutputStream output = null;
  15. while((entry = zipInput.getNextEntry()) != null){
  16. System.out.println("解压缩" + entry.getName() + "文件");
  17. outFile = new File(outzippath + File.separator + entry.getName());
  18. if(!outFile.getParentFile().exists()){
  19. outFile.getParentFile().mkdir();
  20. }
  21. if(!outFile.exists()){
  22. outFile.createNewFile();
  23. }
  24. input = zipFile.getInputStream(entry);
  25. output = new FileOutputStream(outFile);
  26. int temp = 0;
  27. while((temp = input.read()) != -1){
  28. output.write(temp);
  29. }
  30. input.close();
  31. output.close();
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
应用:
[java] view plaincopyprint?
  1. ZipContraMultiFile("f:/zippath.zip", "d:/");
  2. ZipContraMultiFile("d:/hello.zip", "d:/");
 

IO操作之使用zip包压缩和解压缩文件的更多相关文章

  1. [Java 基础] 使用java.util.zip包压缩和解压缩文件

    reference :  http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...

  2. 使用commons-compress操作zip文件(压缩和解压缩)

    http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...

  3. Linux常用命令学习3---(文件的压缩和解压缩命令zip unzip tar、关机和重启命令shutdown reboot……)

    1.压缩和解压缩命令    常用压缩格式:.zip..gz..bz2..tar.gz..tar.bz2..rar .zip格式压缩和解压缩命令        zip 压缩文件名 源文件:压缩文件   ...

  4. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

  5. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...

  6. 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期

    [源码下载] 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的 ...

  7. java采用zip方式实现String的压缩和解压缩CompressStringUtil类

    CompressStringUtil类:不多说,直接贴代码: /** * 压缩 * * @param paramString * @return */ public static final byte ...

  8. Java对zip格式压缩和解压缩

    Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...

  9. Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)

    Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...

随机推荐

  1. PHP程序猿必须学习的第二课——站点安全问题预防

    作为PHP程序猿.第一课我们学习了基本的语法.那么在熟悉基本的语法之后我们应该学些什么呢?我觉得是安全问题.安全问题基于一个站点宛如基石,一着不慎,意味着灾难性的事故. 这里主要就提三点最简单,也是最 ...

  2. HDU4099(斐波那契数列与字典树)

    题目:Revenge of Fibonacci 题意:给出斐波那契数列的前k位,k不超过40,找出最小的正整数n,满足F(n)的前k位与给定数的前k位相同,斐波那契数列的项数不超过100000. 解析 ...

  3. openstack之网络基础

    L1:物理层L2:数据链路层,基于mac地址的通信,通过交换机连接:对等传输,即交换机上的一个主机发一个包,连接在该交换机上的所有机器都能收到:L3:网络层,基于ip地址,路由器设备,连接不同网段,进 ...

  4. 一种解决h5页面背景音乐不能自动播放的方案

    场景:微信.浏览器.App 普通解决方案:采用audio标签的autoplay属性 现象: 大部分IOS系统和少部分Android微信不支持自动播放 $解决方案:监听WeixinJSBridgeRea ...

  5. python中的异常如何处理

    一.异常基础 在编程程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面. try: #正常逻辑代码 input = raw_input("输入数字:") data ...

  6. [LeetCode]题解(python):028-Implement strStr()

    题目来源: https://leetcode.com/problems/implement-strstr/ 题意分析: 输入两个字符串haystack和needle,如果needle是haystack ...

  7. (IOS)截图Demo

    思路是建一个UIView的子类,获取划动出的矩形,用协议将矩形传递给代理对象,依据该矩形完成图像数据的截取,并显示出来. 截图视图类: #import <UIKit/UIKit.h> @p ...

  8. 一維條碼編碼規則(1D Barcode)

    1.Code 39 條碼:又分 標準型Code 39 條碼(Standard Code 39):資料內容包含有0~9數字,A~Z英文字母,”+”,”-“,”*”,”/”,”%”,”$”,”.”以及sp ...

  9. Mylyn

    Mylyn(旧称Mylar)是eclipse的一个插件,用于将任务管理和上下文管理无缝集成到Eclipse中.1. 安装 下载相应的Mylyn zip包,解压缩开就是两个文件夹:features和pl ...

  10. 结构体struct和联合体union以及enum枚举体5的区别

    下面来自wikipedia: In computer science, a union is a value that may have any of several representations ...