在Java中操作Zip文件
来源:互联网 发布:hm有淘宝旗舰店吗 编辑:程序博客网 时间:2024/05/16 11:34
在Java中操作Zip文件,压缩/解压
package test.nothing;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import com.beaconsystem.util.*;
import junit.framework.TestCase;
/**
* @author SonyMusic
*
* 用于测试java.util.zip包压缩和解压缩文件zip文件的例子.
* 基于JUnit编写,包括两个test方法,和三个辅助方法.
* 注重到使用过程中操作的全是流,所以不仅仅可以读写文件。这只是一个简单的例子.
*/
public class TestZipOp extends TestCase {
/**
* ConstrUCtor for TestZipOp.
* @param arg0
*/
public TestZipOp(String arg0) {
super(arg0);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TestZipOp.class);
}
/**
* zip压缩功能测试.
* 将d://temp//zipout目录下的所有文件连同子目录压缩到d://temp//out.zip.
* @throws Exception
*/
public void testCreateZip() throws Exception{
//压缩baseDir下所有文件,包括子目录
String baseDir="d://temp//zipout";
List fileList=getSubFiles(new File(baseDir));
//压缩文件名
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream("d://temp//out.zip"));
ZipEntry ze=null;
byte[] buf=new byte[1024];
int readLen=0;
for (int i = 0; i <fileList.size(); i++) {
File f=(File)fileList.get(i);
System.out.print("Adding: "+f.getPath()+f.getName());
//创建一个ZipEntry,并设置Name和其它的一些属性
ze=new ZipEntry(getAbsFileName(baseDir, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
//将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
InputStream is=new BufferedInputStream(new FileInputStream(f));
while ((readLen=is.read(buf, 0, 1024))!=-1) {
zos.write(buf, 0, readLen);
}
is.close();
System.out.println(" done...");
}
zos.close();
}
/**
* 测试解压缩功能.
* 将d://download//test.zip文件解压到d://temp//zipout目录下.
* @throws Exception
*/
public void testReadZip() throws Exception{
//InputStream is=new BufferedInputStream(new FileInputStream());
String baseDir="d://temp//zipout";
ZipFile zfile=new ZipFile("d://download//Test.zip");
System.out.println(zfile.getName());
Enumeration zList=zfile.entries();
ZipEntry ze=null;
byte[] buf=new byte[1024];
while(zList.hasMoreElements()){
//从ZipFile中得到一个ZipEntry
资料引用:http://www.knowsky.com/364603.html
- 在Java中操作Zip文件
- java-在Java中操作Zip文件,压缩/解压
- java-在Java中操作Zip文件,压缩/解压
- java-在Java中操作Zip文件,压缩/解压
- 在java中解压缩一个zip文件
- 在 Java 中解压 zip 文件
- JAVA操作ZIP文件 -
- java zip文件操作
- Java 对zip文件中含有中文子文件操作
- 在Java代码中使用ZipFile解压zip文件
- java代码操作zip文件--写入zip文件
- java代码操作zip文件--读取zip文件
- Java 中 解压缩文件 ZIP
- ABAP中ZIP文件的操作
- ABAP中ZIP文件的操作
- Java使用基本JDK操作ZIP文件
- Java对zip文件的各种操作
- 在Symbian OS中读取ZIP文件
- 超越“伴侣”
- 研究个MD5算法,搞了二天
- 冒泡法排序
- 曾经在qq空间上留下的文字,美丽,青涩
- 开始了...走到社会!
- 在Java中操作Zip文件
- http/apache工作原理
- Strategy模式
- 人类思维的逻辑结构和算法实现
- 什么是Web2.0
- 顾问成长(10)-客户永远是顾问最好的老师
- [转]Linux之旅(1): diff, patch和quilt (上)
- 实践出真知
- 传感器网络