java.util.zip 用法_使用java.util.zip压缩、解压文件

import java.util.zip.*;

import java.io.*;

public class ZipTest {

public static void main(String[] args) {

if (args.length < 1) {

System.out.println("请输入参数:javac ZipTest 需压缩的文件名及路径 保存的文件名及路径。(e.g. c:/test.txt)");

} else {

ZipCompressor compressor = new ZipCompressor();

boolean isSuccessful= compressor.zipCompress(args, args[args.length - 1]);

if (isSuccessful) {

System.out.println("文件压缩成功。");

} else {

System.out.println("文件压缩失败。");

}

}

}

}

class ZipCompressor {

public ZipCompressor() {}

/**@param srcFiles 需压缩的文件路径及文件名

* @param desFile 保存的文件名及路径

* @return 如果压缩成功返回true

*/

public boolean zipCompress(String[] srcFiles, String desFile) {

boolean isSuccessful = false;

String[] fileNames = new String[srcFiles.length-1];

for (int i = 0; i < srcFiles.length-1; i++) {

fileNames[i] = parse(srcFiles[i]);

}

try {

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));

ZipOutputStream zos = new ZipOutputStream(bos);

String entryName = null;

entryName = fileNames[0];

for (int i = 0; i < fileNames.length; i++) {

entryName = fileNames[i];

// 创建Zip条目

ZipEntry entry = new ZipEntry(entryName);

zos.putNextEntry(entry);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFiles[i]));

byte[] b = new byte[1024];

while (bis.read(b, 0, 1024) != -1) {

zos.write(b, 0, 1024);

}

bis.close();

zos.closeEntry();

}

zos.flush();

zos.close();

isSuccessful = true;

} catch (IOException e) {

}

return isSuccessful;

}

// 解析文件名

private String parse(String srcFile) {

int location = srcFile.lastIndexOf("/");

String fileName = srcFile.substring(location + 1);

return fileName;

}

}

解压文件:

import java.util.zip.*;

import java.io.*;

public class UnzipTest {

public static void main(String[] args) {

if (args.length != 1) {

System.out.println("请输入正确参数:java UnzipTest 需解压的文件(e.g. d:/test.zip)");

} else {

Unzip unzip = new Unzip();

if (unzip.unzip(args[0])) {

System.out.println("文件解压成功。");

} else {

System.out.println("文件解压失败。");

}

}

}

}

class Unzip {

public Unzip() {}

/*

* @param srcZipFile 需解压的文件名

* @return 如果解压成功返回true

*/

public boolean unzip(String srcZipFile) {

boolean isSuccessful = true;

try {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcZipFile));

ZipInputStream zis = new ZipInputStream(bis);

BufferedOutputStream bos = null;

//byte[] b = new byte[1024];

ZipEntry entry = null;

while ((entry=zis.getNextEntry()) != null) {

String entryName = entry.getName();

bos = new BufferedOutputStream(new FileOutputStream("d:/" + entryName));

int b = 0;

while ((b = zis.read()) != -1) {

bos.write(b);

}

bos.flush();

bos.close();

}

zis.close();

} catch (IOException e) {

isSuccessful = false;

}

return isSuccessful;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值