Java实现zip、tar、打包压缩解压
什么是zip
zip是⼀种数据压缩和⽂档储存的⽂件格式,原名Deflate,发明者为菲尔·卡茨(Phil Katz),他于1989年1⽉公布了该格式的资料。
ZIP通常使⽤后缀名“.zip”,它的MIME格式为application/zip。当前,ZIP格式属于⼏种主流的压缩格式之⼀,其竞争者包括RAR 格式以及开放源码的7z格式。从性能上⽐较,RAR及7z格式较ZIP格式压缩率较⾼,⽽7-Zip由于提供了免费的压缩⼯具⽽逐渐在更多的领域得到应⽤。Microsoft从Windows ME操作系统开始内置对zip格式的⽀持,即使⽤户的计算机上没有安装解压缩软件,也能打开和制作zip格式的压缩⽂件,OS X和流⾏的Linux操作系统也对zip格式提供了类似的⽀持。因此如果在⽹络上传播和分发⽂件,zip格式往往是最常⽤的选择。
ZIP是⼀种相当简单的分别压缩每个⽂件的存档格式。分别压缩⽂件允许不必读取另外的数据⽽检索独⽴的⽂件;理论上,这种格式允许对不同的⽂件使⽤不同的算法。不管⽤何种⽅法,对这种格式的⼀个告诫是对于包含很多⼩⽂件的时候,存档会明显的⽐压缩成⼀个独⽴的⽂件(在类Unix系统中⼀个经典的例⼦是普通的存档是由⼀个使⽤gzip压缩的TAR存档组成)要⼤.
什么是tar
tar是UNIX/Linux系统上的压缩⽂件格式,tar⽂件则是⼀种压缩⽂件,在Linux系统中可以直接解压使⽤这种压缩⽂件。在Windows 下也可以使⽤WinRAR等常见的解压缩软件打开。tar其实就相当于常见的rar和zip格式。
什么是
以.为扩展名的是⼀种压缩⽂件,在Linux和OSX下常见,Linux和OSX都可以直接解压使⽤这种压缩⽂件。windows下的WinRAR也可以使⽤。相当于常见的RAR和ZIP格式
总结
zip 和 都属于压缩⽂件,⽂件⼤⼩肯定压缩前⼩
tar 只是属于打包⽂件 ⽂件⼤⼩不会变⼩有可能更⼤
实现代码
package;
import Console;
import TarArchiveEntry;
import TarArchiveInputStream;
import TarArchiveOutputStream;
import ZipArchiveEntry;
import ZipArchiveInputStream;
import ZipArchiveOutputStream;
import GzipCompressorInputStream;
import GzipCompressorOutputStream;
import IOUtils;
import*;
/**
* @ClassName PackAndCompressionUtils
* @Description: 解压压缩⽂件
* @Author drj
* @Date 2021/9/18
* @Version V1.0
**/
public class PackAndCompressionUtils {
private PackAndCompressionUtils(){
private PackAndCompressionUtils(){
}
/**
* 这个⽅法主要针对⽂件实现打包tar 也可以⽣成 但是失去了gz 压缩功能只是现实了打包。
*
* @param filesPathArray 需要打包的⽂件
* @param targetDirPath ⽣成tar ⽬录
* @return
* @throws Exception
*/
public static boolean tarPack(String[] filesPathArray, String targetDirPath)throws Exception {
try(FileOutputStream fileOutputStream =new FileOutputStream(new File(targetDirPath));
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(fileOutputStream);
TarArchiveOutputStream tarArchiveOutputStream =new TarArchiveOutputStream(bufferedOutputStream)){ for(String filePath : filesPathArray){
try(FileInputStream fileInputStream =new FileInputStream(new File(filePath))){
File file =new File(filePath);
TarArchiveEntry tae =new TarArchiveEntry(file, Name());
tarArchiveOutputStream.putArchiveEntry(tae);
tarArchiveOutputStream.cloArchiveEntry();
}catch(Exception e){
<(e,"异常信息:{}", filePath);
}
}
}catch(Exception e){
<(e,"异常信息:{}", filesPathArray);
}
return true;
}
/**
* 解压打包⽂件
*
* @param unPackFilePath
* @param targetDirPath
* @return
* @throws Exception
*/
public static boolean tarUnpack(String unPackFilePath, String targetDirPath)throws Exception {
try(FileInputStream fileInputStream =new FileInputStream(new File(unPackFilePath));
TarArchiveInputStream tarArchiveInputStream =new TarArchiveInputStream(fileInputStream)){
TarArchiveEntry tae =null;
while((tae = NextTarEntry())!=null){
String dir = targetDirPath + File.parator + Name();
try(FileOutputStream fileOutputStream =new FileOutputStream(new File(dir));
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(fileOutputStream)){
}catch(Exception ex){
<(ex,"异常⽂件:{}", dir);
}
}
}catch(Exception ex){
<(ex,"异常⽂件:{}", unPackFilePath);
}
return true;
}
/**
* gzip压缩⽂件后缀是.gz精巧的反义词
*
* @param filesPathArray
* @param targetDirPath
* @return
* @throws IOException
固伦和敬公主
*/
public static boolean gzipCompress(String[] filesPathArray, String targetDirPath)throws IOException {
try(OutputStream outputStream =new FileOutputStream(new File(targetDirPath));
try(OutputStream outputStream =new FileOutputStream(new File(targetDirPath));
GzipCompressorOutputStream gzipCompressorOutputStream =new GzipCompressorOutputStream(outputStream)){ for(String filePath : filesPathArray){
try(InputStream inputStream =new FileInputStream(new File(filePath));){
左眼皮跳跳原唱TarArchiveOutputStream tarArchiveOutputStream =new TarArchiveOutputStream(gzipCompressorOutputStream); File file =new File(filePath);
TarArchiveEntry archiveEntry =new TarArchiveEntry(file, Name());
tarArchiveOutputStream.putArchiveEntry(archiveEntry);
tarArchiveOutputStream.cloArchiveEntry();
}catch(Exception ex){
<(ex,"异常⽂件:{}", filePath);
}
}
}catch(Exception ex){
<(ex,"异常⽂件:{}", filesPathArray);
}
return true;
}
/**
* 针对gz包进⾏解压
*
* @param sourceDir
* @param targetDirPath
*/
public static boolean gzipDeCompress(String sourceDir, String targetDirPath){
丝瓜是黄瓜吗
try(
FileInputStream fileInputStream =new FileInputStream(sourceDir);
GzipCompressorInputStream gzipCompressorInputStream =new GzipCompressorInputStream(fileInputStream);
TarArchiveInputStream tarArchiveInputStream =new TarArchiveInputStream(gzipCompressorInputStream);
){
TarArchiveEntry tarArchiveEntry =null;
while((tarArchiveEntry = NextTarEntry())!=null){
String dir = targetDirPath + File.parator + Name();
try(FileOutputStream fileOutputStream =new FileOutputStream(new File(dir))){
}catch(Exception ex){
<(ex,"异常⽂件路径:{}", sourceDir);
}
}
}catch(Exception ex){
<(ex,"异常⽂件路径:{}", sourceDir);
}
return true;
}
/**
* 对⽂件进⾏zip打包处理
*
* @param filesPathArray
* @param targetDirPath
* @return
* @throws Exception
*/
public static boolean zipCompress(String[] filesPathArray, String targetDirPath)throws Exception {
try(FileOutputStream fileOutputStream =new FileOutputStream(new File(targetDirPath));
ZipArchiveOutputStream zipArchiveOutputStream =new ZipArchiveOutputStream(fileOutputStream)){ for(String filePath : filesPathArray){
try(FileInputStream fileInputStream =new FileInputStream(new File(filePath))){
File file =new File(filePath);
ZipArchiveEntry zae =new ZipArchiveEntry(file, Name());
ZipArchiveEntry zae =new ZipArchiveEntry(file, Name());
zipArchiveOutputStream.putArchiveEntry(zae);
zipArchiveOutputStream.cloArchiveEntry();
}catch(Exception ex){
<(ex,"异常⽂件路径:{}", filePath);
}
}
}catch(Exception ex){
<(ex,"异常⽂件路径:{}", filesPathArray);
}
return true;
}
/**
* 解压zip ⽂件
*
* @param decompressFilePath
* @param targetDirPath
* @return
含羞草的花语* @throws Exception
*/
public static boolean zipDecompress(String decompressFilePath, String targetDirPath)throws Exception {
try(FileInputStream fileInputStream =new FileInputStream(new File(decompressFilePath));
ZipArchiveInputStream zipArchiveInputStream =new ZipArchiveInputStream(fileInputStream)){
ZipArchiveEntry zae =null;
while((zae = NextZipEntry())!=null){
String dir = targetDirPath + File.parator + Name();
try(FileOutputStream fileOutputStream =new FileOutputStream(new File(dir))){
}catch(Exception ex){
<(ex,"异常⽂件路径:{}", dir);
}
}
}catch(Exception ex){
<(ex,"异常⽂件路径:{}", decompressFilePath);
}
return true;
}
}
单元测试
String[] filesPathArray =new String[]{"D:\\core\\a.txt","D:\\core\\b.txt"};
String[] fileTarPath =new String[]{"D:\\core\\tarcompress.tar"};
String compressTarTargetDirPath ="D:\\core\\";
String compressZipTargetDirPath ="D:\\core\\tarcompress.zip";
String compressGzTargetDirPath ="D:\\core\\";
String compressGzTargetDirPath4 ="D:\\core\\";
String deCompressDirPath ="D:\\core";
@Test
public void testTarPack()throws Exception {
boolean b = PackAndCompressionUtils.tarPack(filesPathArray, compressTarTargetDirPath);
Asrt.asrtTrue(b);
}
@Test
public void testTarUnpack()throws Exception {
boolean b = PackAndCompressionUtils.tarUnpack(compressGzTargetDirPath, deCompressDirPath);
三只小熊歌曲
Asrt.asrtTrue(b);
}
@Test美女露出奶奶
public void testZipCompress()throws Exception {
boolean b = PackAndCompressionUtils.zipCompress(filesPathArray, compressZipTargetDirPath);
Asrt.asrtTrue(b);
}
@Test
public void testZipDeCompress()throws Exception {
boolean b = PackAndCompressionUtils.zipDecompress(compressZipTargetDirPath, deCompressDirPath); Asrt.asrtTrue(b);
}
@Test
public void test()throws Exception {
MyUnTarGzUtil.unTarGz(compressGzTargetDirPath,deCompressDirPath);
}
@Test
public void testGzCompress()throws Exception {
boolean b = ipCompress(filesPathArray, compressGzTargetDirPath4);
陈皮皮斗争Asrt.asrtTrue(b);
}
@Test
public void testGzDeCompress()throws Exception {
boolean b = ipDeCompress(compressTarTargetDirPath, deCompressDirPath); Asrt.asrtTrue(b);
}