java 7z_java实现zip,gzip,7z,zlib格式的压缩打包

前言

利用Java原生类和apache的commons实现zip,gzip,7z,zlib的压缩打包

maven依赖

org.apache.commons

commons-compress

1.12

zip格式

public static void zip(String input, String output, String name) throws Exception {

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(output));

String[] paths = input.split("\\|");

File[] files = new File[paths.length];

byte[] buffer = new byte[1024];

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

files[i] = new File(paths[i]);

}

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

FileInputStream fis = new FileInputStream(files[i]);

if (files.length == 1 && name != null) {

out.putNextEntry(new ZipEntry(name));

} else {

out.putNextEntry(new ZipEntry(files[i].getName()));

}

int len;

// 读入需要下载的文件的内容,打包到zip文件

while ((len = fis.read(buffer)) > 0) {

out.write(buffer, 0, len);

}

out.closeEntry();

fis.close();

}

out.close();

}

gzip打包

public static void gzip(String input, String output, String name) throws Exception {

String compress_name = null;

if (name != null) {

compress_name = name;

} else {

compress_name = new File(input).getName();

}

byte[] buffer = new byte[1024];

try {

GzipParameters gp = new GzipParameters(); //设置压缩文件里的文件名

gp.setFilename(compress_name);

GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new FileOutputStream(output), gp);

FileInputStream fis = new FileInputStream(input);

int length;

while ((length = fis.read(buffer)) > 0) {

gcos.write(buffer, 0, length);

}

fis.close();

gcos.finish();

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

7z打包

public static void z7z(String input, String output, String name) throws Exception {

try {

SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(output));

SevenZArchiveEntry entry = null;

String[] paths = input.split("\\|");

File[] files = new File[paths.length];

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

files[i] = new File(paths[i].trim());

}

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

BufferedInputStream instream = null;

instream = new BufferedInputStream(new FileInputStream(paths[i]));

if (name != null) {

entry = sevenZOutput.createArchiveEntry(new File(paths[i]), name);

} else {

entry = sevenZOutput.createArchiveEntry(new File(paths[i]), new File(paths[i]).getName());

}

sevenZOutput.putArchiveEntry(entry);

byte[] buffer = new byte[1024];

int len;

while ((len = instream.read(buffer)) > 0) {

sevenZOutput.write(buffer, 0, len);

}

instream.close();

sevenZOutput.closeArchiveEntry();

}

sevenZOutput.close();

} catch (IOException ioe) {

System.out.println(ioe.toString() + " " + input);

}

}

zlib打包

public static void zlib(String input, String output) throws Exception {

// DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(output));

DeflateParameters dp = new DeflateParameters();

dp.setWithZlibHeader(true);

DeflateCompressorOutputStream dcos = new DeflateCompressorOutputStream(new FileOutputStream(output),dp);

FileInputStream fis = new FileInputStream(input);

int length = (int) new File(input).length();

byte data[] = new byte[length];

// int length;

while ((length = fis.read(data)) > 0) {

dcos.write(data, 0, length);

}

fis.close();

dcos.finish();

dcos.close();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值