java 快速压缩_怎样用java快速实现zip文件的压缩解压缩?(给分20!)

我使用两种方法从zip文件中读取数据,第一种的代码是从“UTF开始”到“UTF结束”,看到有人(http://www.csdn.net/develop/article/6839.shtm)介绍过这种用法,但是我用的时候,出现java.io.UTFDateFormatException异常,我跟踪调试的时候,发现问题出现在读取的时候,写是可以的。

第二种的代码是从“int开始”到“int结束”,可以正确解压缩各种文件(二进制读取,应该也没问题的),但是速度很慢,请问各位大虾,有没有什么办法解决?

代码如下:

……

String doc="";

zin = new ZipInputStream(new FileInputStream(待解压缩文件));

while(((entry = zin.getNextEntry()) != null)&&!entry.isDirectory())

{

FileOutputStream fout =

new FileOutputStream(解压缩后的文件名);

DataOutputStream dout = new DataOutputStream(fout);

DataInputStream in = new DataInputStream(zin);

/*

//UTF开始

doc=in.readUTF();

in.close();

dout.writeUTF(doc);

dout.close();

//UTF结束

*/

//int开始

int c;

while((c = in.read()) != -1)

dout.write(c);

dout.close();

//int结束

fout.close();

zin.closeEntry();

System.out.println("Close entry successful!");

}

zin.close();

……

|

/*

** a simple unZIP tool

**

** ex.  java UnZip file.zip file1   to unzip file 1 from file.zip

**      java UnZip file.zip         to unzip file.zip

**

*/

import java.io.*;

import java.util.*;

import java.util.zip.*;

import java.text.*;

class UnZip {

public static void main(String args[]) throws IOException {

InputStream in = new BufferedInputStream(new FileInputStream(args[0]));

ZipInputStream zin = new ZipInputStream(in);

ZipEntry e;

while((e=zin.getNextEntry())!= null) {

if (args.length > 1) {

if (e.getName().equals(args[1])) {

unzip(zin, args[1]);

break;

}

}

unzip(zin, e.getName());

}

zin.close();

}

public static void unzip(ZipInputStream zin, String s) throws IOException {

System.out.println("unzipping " + s);

FileOutputStream out = new FileOutputStream(s);

byte [] b = new byte[512];

int len = 0;

while ( (len=zin.read(b))!= -1 ) {

out.write(b,0,len);

}

out.close();

}

}

===================================================

/*

** a simple ZIP tool

**

** ex.  java Zip file.1 file.2 > file.zip

**

*/

import java.io.*;

import java.util.zip.*;

class Zip {

public static void main(String args[]) throws IOException {

byte b[] = new byte[512];

ZipOutputStream zout = new ZipOutputStream(System.out);

for(int i = 0; i  0) {

long csize = e.getCompressedSize();

long ratio = ((size-csize)*100) / size;

err.println(" (deflated " + ratio + "%)");

}

else {

err.println(" (deflated 0%)");

}

}

else {

err.println(" (stored 0%)");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值