java 分卷 zip

最近想写个东西,要使用分卷压缩,但是不想使用别的开源包,自己写了一个类来实现,其实使用

public void zip(String path) throws IOException {
File file = new File(path);

BufferedInputStream origin = null;
byte data[] = new byte[BUFFER];
FileInputStream fi = new FileInputStream(file);
origin = new BufferedInputStream(fi, BUFFER);
int count;
int offset = 0;
ZipOutputStream out = null;
while (true) {
if ((count = origin.read(data, 0, BUFFER)) != -1) {
if (offset % (MEGA) == 0) {
FileOutputStream dest = new FileOutputStream(file.getName()
+ ".(part" + (offset / (MEGA) + 1) + ").zip");
out = new ZipOutputStream(new BufferedOutputStream(dest));
ZipEntry entry = new ZipEntry(file.getName());
out.putNextEntry(entry);
}
out.write(data, 0, count);
offset += 1024;
if (offset % (MEGA) == 0 && offset != 0) {
out.close();
}
} else {
out.close();
break;
}
}
origin.close();
}

public void unzip(String[] path) throws IOException {
File file = new File(path[0].split("\\.")[0] + "."
+ path[0].split("\\.")[1]);

BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
byte data[] = new byte[BUFFER];
int offset = 0;
for (int i = 0; i < path.length; i++) {
ZipInputStream unzipfile = new ZipInputStream(new FileInputStream(
path[0]));
BufferedInputStream in = new BufferedInputStream(unzipfile);
ZipEntry entry = unzipfile.getNextEntry();
int count = 0;
while ((count = in.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
in.close();
}
out.close();
}

这里不要忘记了,压缩和解压都要使用entry来获得该文件的入口ZipEntry提供了一个功能很广泛的接口,允许我们获取设置zip文件内该特定项上的可利用的数据:名字压缩和未压缩的文件大小,日期校验和等

转载于:https://www.cnblogs.com/alabasta/archive/2012/04/05/2362394.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值