Apache commons compress文件打包、压缩
2018-03-24 01:00:31
1078次阅读
0个评论
Apache commons compress BZIP2压缩:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
public class CommonsBZip2Compress extends Compress {
@Override
protected void doCompress(File srcFile, File destFile) throws IOException {
OutputStream out = null;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen);
out = new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen));
IOUtils.copy(is, out);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(out);
}
}
@Override
protected void doDecompress(File srcFile, File destDir) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.toString()));
is = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen));
os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen);
IOUtils.copy(is, os);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
}
}
Apache commons compress GZIP压缩:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
public class CommonsGZIPCompress extends Compress {
@Override
protected void doCompress(File srcFile, File destFile) throws IOException {
OutputStream out = null;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen);
out = new BZip2CompressorOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen));
IOUtils.copy(is, out);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(out);
}
}
@Override
protected void doDecompress(File srcFile, File destDir) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.toString()));
is = new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen));
os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen);
IOUtils.copy(is, os);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
}
}
Apache commons compress ZIP压缩
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.IOUtils;
public class CommonsZipCompress extends Compress {
/**用于单文件压缩*/
@Override
protected void doCompress(File srcFile, File destFile) throws IOException {
ZipArchiveOutputStream out = null;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen);
out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen));
ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());
entry.setSize(srcFile.length());
out.putArchiveEntry(entry);
IOUtils.copy(is, out);
out.closeArchiveEntry();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(out);
}
}
@Override
protected void doDecompress(File srcFile, File destDir) throws IOException {
ZipArchiveInputStream is = null;
try {
is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen));
ZipArchiveEntry entry = null;
while ((entry = is.getNextZipEntry()) != null) {
if (entry.isDirectory()) {
File directory = new File(destDir, entry.getName());
directory.mkdirs();
} else {
OutputStream os = null;
try {
os = new BufferedOutputStream(
new FileOutputStream(new File(destDir, entry.getName())), bufferLen);
IOUtils.copy(is, os);
} finally {
IOUtils.closeQuietly(os);
}
}
}
} finally {
IOUtils.closeQuietly(is);
}
}
}
Apache commons compress AR打包:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
import org.apache.commons.compress.archivers.ar.ArArchiveOutputStream;
import org.apache.commons.io.IOUtils;
public class CommonsArCompress extends Compress {
@Override
protected void doCompress(File srcFile, File destFile) throws IOException {
ArArchiveOutputStream zout = null;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen);
zout = new ArArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen));
zout.putArchiveEntry(new ArArchiveEntry(srcFile, srcFile.getName()));
IOUtils.copy(is, zout);
zout.closeArchiveEntry();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(zout);
}
}
@Override
protected void doDecompress(File srcFile, File destDir) throws IOException {
ArArchiveInputStream is = null;
try {
is = new ArArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen));
ArArchiveEntry entry = null;
while ((entry = is.getNextArEntry()) != null) {
if (entry.isDirectory()) {
File directory = new File(destDir, entry.getName());
directory.mkdirs();
} else {
BufferedOutputStream bos = null;
try {
byte[] buffer = new byte[bufferLen];
bos = new BufferedOutputStream(new FileOutputStream(
new File(destDir, entry.getName())), bufferLen);
int len;
long size = entry.getSize();
while (size > 0) {
if (size < bufferLen) {
len = is.read(buffer, 0, (int) size);
size -= len;
} else {
len = is.read(buffer);
size -= len;
}
bos.write(buffer, 0, len);
}
} finally {
IOUtils.closeQuietly(bos);
}
}
}
} finally {
IOUtils.closeQuietly(is);
}
}
}
Apache commons compress CPIO打包:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
import org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream;
import org.apache.commons.io.IOUtils;
public class CommonsCPIOCompress extends Compress {
@Override
protected void doCompress(File srcFile, File destFile) throws IOException {
CpioArchiveOutputStream out = null;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen);
out = new CpioArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen));
out.putArchiveEntry(new CpioArchiveEntry(srcFile, srcFile.getName()));
IOUtils.copy(is, out);
out.closeArchiveEntry();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(out);
}
}
@Override
protected void doDecompress(File srcFile, File destDir) throws IOException {
CpioArchiveInputStream is = null;
try {
is = new CpioArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen));
CpioArchiveEntry entry = null;
while ((entry = is.getNextCPIOEntry()) != null) {
if (entry.isDirectory()) {
File directory = new File(destDir, entry.getName());
directory.mkdirs();
} else {
BufferedOutputStream bos = null;
try {
byte[] buffer = new byte[bufferLen];
bos = new BufferedOutputStream(new FileOutputStream(
new File(destDir, entry.getName())), bufferLen);
int len;
long size = entry.getSize();
while (size > 0) {
if (size < bufferLen) {
len = is.read(buffer, 0, (int) size);
size -= len;
} else {
len = is.read(buffer);
size -= len;
}
bos.write(buffer, 0, len);
}
} finally {
IOUtils.closeQuietly(bos);
}
}
}
} finally {
IOUtils.closeQuietly(is);
}
}
}
Apache commons compress TAR打包
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.IOUtils;
public class CommonsTarCompress extends Compress {
@Override
protected void doCompress(File srcFile, File destFile) throws IOException {
TarArchiveOutputStream out = null;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen);
out = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen));
TarArchiveEntry entry = new TarArchiveEntry(srcFile.getName());
entry.setSize(srcFile.length());
out.putArchiveEntry(entry);
IOUtils.copy(is, out);
out.closeArchiveEntry();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(out);
}
}
@Override
protected void doDecompress(File srcFile, File destDir) throws IOException {
TarArchiveInputStream is = null;
try {
is = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen));
TarArchiveEntry entry = null;
while ((entry = is.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
File directory = new File(destDir, entry.getName());
directory.mkdirs();
} else {
BufferedOutputStream bos = null;
try {
byte[] buffer = new byte[bufferLen];
bos = new BufferedOutputStream(new FileOutputStream(
new File(destDir, entry.getName())), bufferLen);
int len;
long size = entry.getSize();
while (size > 0) {
if (size < bufferLen) {
len = is.read(buffer, 0, (int) size);
size -= len;
} else {
len = is.read(buffer);
size -= len;
}
bos.write(buffer, 0, len);
}
} finally {
IOUtils.closeQuietly(bos);
}
}
}
} finally {
IOUtils.closeQuietly(is);
}
}
}
00
相关话题
- Apache Commons Compress zip压缩解压
- Apache-Commons CSV读写文件
- Apache的commons-net实现FTP的文件上传下载
- Maven打包war包含空文件夹
- Apache POI 3.17读取PowerPoint文件PPT
- zip4j压缩、解压zip文件
- 使用commons-vfs监听文件系统
- Apache Commons JEXL实现字符串转换成可执行代码
- 使用commons-io-2.0监听文件变化
- SImpleImage解决图片压缩变红问题
- 前端图片压缩与上传OSS组件
- Spring boot打包后从类路径目录中获取资源列表
- Vue2.0 移动端拍照压缩图片预览及上传
- Linux上使用commons-net.jar注意的问题
- commons-lang3提示“The type StringEscapeUtils is deprecated”