1.生成zip

public static void main(String[] args) {
try {
// testZip("c:\\temp.txt", "c:\\temp4.zip");
// testZip("c:\\Result.txt", "c:\\temp4.zip"); //不然会被一个文件覆盖了. //压缩多个文件的关键: ZipOutputStream out 作为参数传递.
//一个流,否则存在覆盖的问题,即每次会new一个,所以外置.
ZipOutputStream zos = null;
zos = new ZipOutputStream(new FileOutputStream("c:\\temp5.zip")); testZip("c:\\temp.txt",zos);
testZip("c:\\Result.txt",zos); //外置
zos.closeEntry();
zos.close(); } catch (Exception e) {
e.printStackTrace();
}
} /**
* 重复压缩文件
* @param zipEntryName // 去掉String zipEntryName,
* @param filePath
* @param zipPath
* @throws Exception
*/
public static void testZip(String filePath,ZipOutputStream zos) throws Exception{ //压缩包
/*ZipOutputStream zos = null;*/ // BufferedOutputStream bos = null;
// File zipFile = new File(zipPath);
// if(zipFile.exists()==false){
/* zos = new ZipOutputStream(new FileOutputStream(zipPath));*/ // bos = new BufferedOutputStream(zos); //Buffer...
// }else{
//
// } File f = new File(filePath); // //create zip
FileInputStream fis = new FileInputStream(f);
// BufferedInputStream bis = new BufferedInputStream(fis); // set the file name in the .zip file
// zos.putNextEntry(new ZipEntry(zipEntryName));
zos.putNextEntry(new ZipEntry(f.getName()));
// set the declear
zos.setComment("by zip test!"); // byte[] b = new byte[1024];
// while (true) {
// int len = bis.read(b);
// if (len == -1)
// break;
// bos.write(b, 0, len);
// System.out.println(new String(b, 0, len));
// }
// bos.flush(); //这一行重要,否则txt是空白文件. byte[] buffer = new byte[1024];
int len = 0 ;
// 读取文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
} fis.close();
/*zos.closeEntry();
zos.close();*/
}

2.解压缩

public static void main(String[] args) throws Exception {
// get a zip file instance
File file = new File("c:\\temp5.zip"); // get a ZipFile instance
ZipFile zipFile = new ZipFile(file); // create a ZipInputStream instance
ZipInputStream zis = new ZipInputStream(new FileInputStream(file)); // create a ZipEntry instance , lay the every file from
// decompress file temporarily
ZipEntry entry = null; // a circle to get every file
while ((entry = zis.getNextEntry()) != null) {
System.out.println("decompress file :" + entry.getName()); // define the path to set the file
File outFile = new File("c:\\zip\\"
+ entry.getName()); // if the file's parent directory wasn't exits ,than
// create the directory
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdir();
} // if the file not exits ,than create the file
if (!outFile.exists()) {
outFile.createNewFile();
} // create an input stream 读文件
BufferedInputStream bis = new BufferedInputStream(
zipFile.getInputStream(entry)); // create an output stream 写文件
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(outFile));
byte[] b = new byte[1024];
while (true) {
int len = bis.read(b);
if (len == -1)
break;
bos.write(b, 0, len);
}
// close stream
bis.close();
bos.close();
}
zis.close();
}

java生成zip压缩文件,解压缩文件的更多相关文章

  1. java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹

    一.简述 zip用于压缩和解压文件.使用到的类有:ZipEntry  ZipOutputStream 二.具体实现代码 package com.joyplus.test; import java.io ...

  2. PHP生成ZIP压缩文件

    PHP生成ZIP压缩文件 /* * 生成zip压缩文件 * $sourceDir:被压缩的文件夹或文件 * $outFileName:输出的压缩文件名称 * */ function createZip ...

  3. Java 的zip压缩和解压缩

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

  4. java直接生成zip压缩文件精简代码(跳过txt文件)

    /** * @param args */ public static void main(String[] args) throws Exception{ ZipOutputStream zos = ...

  5. Java操作zip压缩和解压缩文件工具类

    需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...

  6. java.util.zip压缩打包文件总结二: ZIP解压技术

    一.简述 解压技术和压缩技术正好相反,解压技术要用到的类:由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如: Checked ...

  7. C#压缩、解压缩文件(夹)(rar、zip)

    主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注 ...

  8. Linux命令(十六) 压缩或解压缩文件和目录 zip unzip

    目录 1.命令简介 2.常用参数介绍 3.实例 4.直达底部 命令简介 zip 是 Linux 系统下广泛使用的压缩程序,文件压缩后扩展名为 ".zip". zip 命令用来将文件 ...

  9. php生成zip压缩文件的方法,支持文件和压缩包路径查找

    /* * new creatZip($_dir,$_zipName); *@ _dir是被压缩的文件夹名称,可使用路径,例 'a'或者'a/test.txt'或者'test.txt' *@ _zipN ...

随机推荐

  1. vb.net播放资源文件中的音乐

    1.在自己的工程里添加一个资源文件. 2.打开添加的资源文件,资源类型选择为音频,点击添加资源把准备好的wav格式音乐文件添加进入资源文件. 3.设置资源属性和文件属性为嵌入 4.代码以及调用方法 P ...

  2. python中 is 和 == 的区别

    is 是比较两个引用是否指向了同一个对象(引用比较). == 是比较两个对象是否相等.

  3. shell脚本使用技巧5--字符分隔

    #!/bin/bash #filename:ifs.sh data="name,sex,rollon,location" oldIFS=$IFS IFS=, for item in ...

  4. Drying [POJ3104] [二分答案]

    背景 每件衣服都有一定单位水分,在不适用烘干器的情况下,每件衣服每分钟自然流失1个单位水分,但如果使用了烘干机则每分钟流失K个单位水分,但是遗憾是只有1台烘干机,每台烘干机同时只能烘干1件衣服,请问要 ...

  5. 网络 [HNOI2016]

    Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有服 ...

  6. 区间dp板子题:[noi1995]石子合并

    非常经典的区间dp模板 对于每一个大于二的区间 我们显然都可以将它拆分成两个子序列 那么分别计算对于每个取最优值即可 #pragma GCC optimize("O2") #inc ...

  7. MQ基本概念

    MQ的基本概念 1) 队列管理器 队列管理器是MQ系统中最上层的一个概念,由它为我们提供基于队列的消息服务. 2) 消息 在MQ中,我们把应用程序交由MQ传输的数据定义为消息,我们可以定义消息的内容并 ...

  8. mysql5.7.20完全卸载 win10

    试了好多方法,搞了很久终于完全卸载干净,重装完成了! 1.关闭服务 以管理员身份运行cmd,执行以下命令: C:\WINDOWS\system32>net stop mysql MySQL 服务 ...

  9. Spring Boot使用Druid连接池基本配置

    以下为Spring Boot配置Druid 一.pom.xml配置 <dependency> <groupId>com.alibaba</groupId> < ...

  10. HTML5 学习01——浏览器问题、新元素

    Internet Explorer 浏览器问题 问题:Internet Explorer 8 及更早 IE 版本的浏览器不支持HTML5的方式. <!--[if lt IE 9]> < ...