java压缩与解压缩,写个文章记录一下

支持gz,zip,jar的压缩与解压缩,用到的jar:commons-compress-1.5.jar、commons-io-2.4.jar、commons-lang3-3.1.jar。话不多说,直接上代码:
public class CompressAndExtractFileUtils {
private final static CompressAndExtractFileUtils instance = new CompressAndExtractFileUtils();
private final static String DEFAULT_ENCODING = "UTF-8";

private CompressAndExtractFileUtils() {
}

public void compressFile(String srcPath, String destPath) {
compressFile(srcPath, destPath, DEFAULT_ENCODING);
}

public void compressFile(String srcPath, String destPath, String encoding) {
FileOutputStream fos = null;
ArchiveOutputStream os = null;
try {
if (StringUtils.isEmpty(encoding)) {
encoding = DEFAULT_ENCODING;
}
File destFile = mkFile(destPath);// mkFile
fos = new FileOutputStream(destFile);
String extension = getExtension(destPath);
os = getArchiveOutputStream(extension, fos, encoding);
compressFile(os, extension, srcPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(fos);
}

}

private void compressFile(ArchiveOutputStream os, String extension, String srcPath) {
String basePath = getBasePath(srcPath);
if ("zip".equals(extension)) {
compressZipFile(os, srcPath, basePath);
} else if ("jar".equals(extension)) {
compressJarFile(os, srcPath, basePath);
} else if ("tar".equals(extension) || "gz".equals(extension)) {
compressTarFile(os, srcPath, basePath);
}

}

private void compressJarFile(ArchiveOutputStream os, String srcPath, String basePath) {
File f = new File(srcPath);
if (f.isFile()) {
FileInputStream fis = null;
try {
JarArchiveEntry entry = new JarArchiveEntry(srcPath.substring(basePath.length()));
os.putArchiveEntry(entry);
fis = new FileInputStream(f);
IOUtils.copy(fis, os);
os.closeArchiveEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fis);
}
} else {
File[] subFile = f.listFiles();
for (File sub : subFile) {
compressJarFile(os, sub.getAbsolutePath(), basePath);
}
}
}

private void compressTarFile(ArchiveOutputStream os, String srcPath, String basePath) {
File f = new File(srcPath);
if (f.isFile()) {
FileInputStream fis = null;
try {
TarArchiveEntry entry = new TarArchiveEntry(f, srcPath.substring(basePath.length()));
os.putArchiveEntry(entry);
fis = new FileInputStream(f);
IOUtils.copy(fis, os);
os.closeArchiveEntry();
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fis);
}
} else {
File[] subFile = f.listFiles();
for (File sub : subFile) {
compressTarFile(os, sub.getAbsolutePath(), basePath);
}
}
}

private void compressZipFile(ArchiveOutputStream os, String srcPath, String basePath) {
File f = new File(srcPath);
if (f.isFile()) {
FileInputStream fis = null;
try {
ZipArchiveEntry entry = new ZipArchiveEntry(srcPath.substring(basePath.length()));
os.putArchiveEntry(entry);
fis = new FileInputStream(f);
IOUtils.copy(fis, os);
os.closeArchiveEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fis);
}
} else {
File[] subFile = f.listFiles();
for (File sub : subFile) {
compressZipFile(os, sub.getAbsolutePath(), basePath);
}
}
}

private ArchiveOutputStream getArchiveOutputStream(String extension, FileOutputStream fos, String encoding) throws Exception {
// create factory
ArchiveStreamFactory factory = new ArchiveStreamFactory();
// set entryEncoding
factory.setEntryEncoding(encoding);
// return ArchiveOutputStream with extension
if ("gz".equals(extension) || "tar".equals(extension)) {
return factory.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);
} else if ("zip".equals(extension)) {
return factory.createArchiveOutputStream(ArchiveStreamFactory.ZIP, fos);
} else if ("jar".equals(extension)) {
return factory.createArchiveOutputStream(ArchiveStreamFactory.JAR, fos);
}
return null;
}


public void extractFile(String srcFile, String destFile) {
extractFile(srcFile, destFile, DEFAULT_ENCODING);
}

public void extractFile(String srcFile, String destFile, String encoding) {
File file = new File(destFile);
if (!file.exists()) {
file.mkdir();
}
FileInputStream fis = null;
ArchiveInputStream in = null;
try {
if (StringUtils.isEmpty(encoding)) {
encoding = DEFAULT_ENCODING;
}
fis = new FileInputStream(srcFile);
String extension = getExtension(srcFile);
in = getArchiveInputStream(fis, extension, encoding);
extractFile(in, extension, destFile);
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(fis);
}
}

private void extractFile(ArchiveInputStream in, String extension, String outputDirectory) {
if ("gz".equals(extension) || "tar".equals(extension)) {
extractFile(in, outputDirectory, TarArchiveEntry.class);
} else if ("zip".equals(extension)) {
extractFile(in, outputDirectory, ZipArchiveEntry.class);
} else if ("jar".equals(extension)) {
extractFile(in, outputDirectory, JarArchiveEntry.class);
}
}

@SuppressWarnings("unchecked")
public <T extends ArchiveEntry> void extractFile(ArchiveInputStream in, String outputDirectory, Class<T> T) {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(in);
T entry = null;
while ((entry = (T) in.getNextEntry()) != null) {
String name = entry.getName();
System.out.println("name:" + name);
String[] names = name.split("/");
String fileName = outputDirectory;
for (int i = 0; i < names.length; i++) {
String str = names[i];
fileName = fileName + File.separator + str;
}
if (name.endsWith("/")) {
mkFolder(fileName);
} else {
BufferedOutputStream bos = null;
try {
File file = mkFile(fileName);
bos = new BufferedOutputStream(new FileOutputStream(file));
IOUtils.copy(in, bos);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(bos);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
} finally {
IOUtils.closeQuietly(bis);
}
}

public void extractZipFile() {

}

private void mkFolder(String fileName) {
System.out.println("make folder[" + fileName + "]");
File f = new File(fileName);
if (!f.exists()) {
f.mkdirs();
}
}

private File mkFile(String fileName) {
System.out.println("make file[" + fileName + "]");
File f = new File(fileName);
try {
File parent = f.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return f;
}

public static CompressAndExtractFileUtils getInstance() {
return instance;
}

public ArchiveInputStream getArchiveInputStream(FileInputStream fis, String extension, String encoding) throws Exception, Throwable {
// create factory
ArchiveStreamFactory factory = new ArchiveStreamFactory();
// set entryEncoding
factory.setEntryEncoding(encoding);
if ("gz".equals(extension) || "tar".equals(extension)) {
return factory.createArchiveInputStream(ArchiveStreamFactory.TAR, new GZIPInputStream(new BufferedInputStream(fis)));
} else if ("zip".equals(extension)) {
return factory.createArchiveInputStream(ArchiveStreamFactory.ZIP, fis);
} else if ("jar".equals(extension)) {
return factory.createArchiveInputStream(ArchiveStreamFactory.JAR, fis);
}
return null;
}

public String getExtension(String path) {
int index = path.lastIndexOf(".");
if (index != -1) {
return path.substring(index + 1, path.length()).toLowerCase();
}
return "";
}

private String getBasePath(String srcPath) {
String basePath = srcPath;
File srcFile = mkFile(srcPath);// mkFile
if (srcFile.isFile()) {
basePath = srcFile.getParent();
} else {
if (!basePath.endsWith("/")) {
basePath += File.separator;
}
}
return basePath;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值