reference :  http://www.open-open.com/lib/view/open1381641653833.html

Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。

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

ZipFile

java中的每一个压缩文件都是可以使用ZipFile来进行表示的。

File file = new File("F:/zippath.zip");
ZipFile zipFile = new ZipFile(file);
System.out.println("压缩文件的名称为:" + zipFile.getName());

压缩单个文件

/** 压缩单个文件*/
public static void ZipFile(String filepath ,String zippath) {
try {
File file = new File(filepath);
File zipFile = new File(zippath);
InputStream input = new FileInputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
input.close();
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}

应用:

ZipFile("d:/hello.txt", "d:/hello.zip");

压缩多个文件(文件夹)

/** 一次性压缩多个文件,文件存放至一个文件夹中*/
public static void ZipMultiFile(String filepath ,String zippath) {
try {
File file = new File(filepath);// 要被压缩的文件夹
File zipFile = new File(zippath);
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
if(file.isDirectory()){
File[] files = file.listFiles();
for(int i = 0; i < files.length; ++i){
input = new FileInputStream(files[i]);
zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
input.close();
}
}
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}

应用:

ZipMultiFile("f:/uu", "f:/zippath.zip");

解压缩单个文件

/**  解压缩(解压缩单个文件)*/
public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
try {
File file = new File(zippath);//压缩文件路径和文件名
File outFile = new File(outfilepath);//解压后路径和文件名
ZipFile zipFile = new ZipFile(file);
ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
InputStream input = zipFile.getInputStream(entry);
OutputStream output = new FileOutputStream(outFile);
int temp = 0;
while((temp = input.read()) != -1){
output.write(temp);
}
input.close();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}

应用:

ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");

解压缩多个文件

ZipInputStream类:
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了。

如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类。

/**  解压缩(压缩文件中包含多个文件)可代替上面的方法使用。
* ZipInputStream类
* 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,
* 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
* */
public static void ZipContraMultiFile(String zippath ,String outzippath){
try {
File file = new File(zippath);
File outFile = null;
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
InputStream input = null;
OutputStream output = null;
while((entry = zipInput.getNextEntry()) != null){
System.out.println("解压缩" + entry.getName() + "文件");
outFile = new File(outzippath + File.separator + entry.getName());
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdir();
}
if(!outFile.exists()){
outFile.createNewFile();
}
input = zipFile.getInputStream(entry);
output = new FileOutputStream(outFile);
int temp = 0;
while((temp = input.read()) != -1){
output.write(temp);
}
input.close();
output.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

应用:

ZipContraMultiFile("f:/zippath.zip", "d:/");
ZipContraMultiFile("d:/hello.zip", "d:/");

[Java 基础] 使用java.util.zip包压缩和解压缩文件的更多相关文章

  1. java中ant包中的org.apache.tools.zip实现压缩和解压缩

    其实apache中的ant包(请自行GOOGLE之ant.jar)中有一个更好的类,已经支持中文了,我们就不重复制造轮子了,拿来用吧,这里最主要的功能是实现了 可以指定多个文件 到同一个压缩包的功能 ...

  2. Java基础--压缩和解压缩gz包

    gz是Linux和OSX中常见的压缩文件格式,下面是用java压缩和解压缩gz包的例子 public class GZIPcompress { public static void FileCompr ...

  3. Java 的zip压缩和解压缩

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

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

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

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

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

  6. java基础-学java util类库总结

    JAVA基础 Util包介绍 学Java基础的工具类库java.util包.在这个包中,Java提供了一些实用的方法和数据结构.本章介绍Java的实用工具类库java.util包.在这个包中,Java ...

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

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

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

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

  9. IO操作之使用zip包压缩和解压缩文件

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

随机推荐

  1. 校友聊NABCD

    特点之一     界面简洁 N:软件的界面是软件成功的必要条件,界面简洁,用户使用方便,就会吸引用户. A:界面可用多种做法做,暂定用C# B:简洁的界面,用户易于理解各项功能,方便使用. C:没有其 ...

  2. Javascript高级程序设计——执行环境与作用域

    Javascript中执行环境是定义了变量或函数有权访问的其他数据,决定了各自的行为,每个执行的环境都有一个与之关联的变量对象,环境中定义的所以变量和函数都保存在这个对象中. 全局执行环境是最外围的一 ...

  3. 又一个绝对棒的对话框插件fancybox v1.3.4

    http://www.jsfoot.com/jquery/demo/2011-07-30/fancybox/index.html jquery插件:fancybox   Fancybox的特点如下: ...

  4. UOJ25——IOI2014Wall

    1.题目大意:这道题也是线段树修改,有两种修改,一个区间中大于h都变成h,一个区间中小于h都变成h,单点询问 主要是这几种操作 2.分析:这道题是双标记,还是父亲的优先级比儿子低,自己用手推推就可以知 ...

  5. C++ 复制构造函数

    C++类的设计中,如果某些函数没有显式定义,C++会自动生成,复制构造函数便是其中之一,其他的还有默认构造函数.赋值操作符.默认析构函数.地址操作符.一个类的复制构造函数的原型一般为: Class_n ...

  6. ionic的页面直接的跳转

    $state.go页面不刷新数据 假如进入market/beian/add添加数据,保存提交后回退market/beian列表页,没有自动更新数据,必须得手动下拉刷新才会出来 $state.go(&q ...

  7. [codeforces 317]A. Perfect Pair

    [codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...

  8. 剑指Offer 连续子数组的最大和

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  9. c_水程序

    * ** This program reads input lines from the standard input and prints ** each input line, followed ...

  10. super用法和继承中的构造方法

    当new出来一个对象的时候,  this是只想对象本身. 在存在继承关系时, 在子类中用super表示引用父类中的东西. 子类的构造过程必须调用父类的构造方法. 子类中包含父类,所以子类中一定要先调用 ...