文件解压缩技术探讨
java解压缩技术主要分为zip、gzip、tar技术等,该文章主要是阐述一下以上三种解压缩技术的具体实现。
一、zip技术的实现
zip技术是java自带的解压缩技术,也是winsdows系统非常常用的一种解压缩技术,在linux系统也无需装任何软件即可以解压。
zip压缩
zip压缩,可以压缩带文件夹的文件,主要是通过递归来对文件夹进行压缩
/**
* zip压缩
* @param sourcePath
* @param targetPath
*/
public static void createZip(String sourcePath, String targetPath) {
//获取该目录下所有文件以及文件夹
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(new File(targetPath))))) {
zipOperation(zipOutputStream,new File(sourcePath),"");
} catch (IOException e) {
log.error("createZip exception", e);
}
}
/**
* 压缩具体操作
* @param zipOutputStream
* @param file
* @param path
*/
private static void zipOperation(ZipOutputStream zipOutputStream,File file,String path){
// 如果是目录,则递归进行处理
if(file.isDirectory()) {
File[] files = file.listFiles();
for (File tempFile : files) {
zipOperation(zipOutputStream, tempFile,path + "/" + tempFile.getName());
}
}
else{
// 如果是单个文件,再进行压缩
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
ZipEntry entry = new ZipEntry(path);
zipOutputStream.putNextEntry(entry);
int len;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
zipOutputStream.closeEntry();
}catch (Exception e){
log.error("zipOperation exception", e);
}
}
}
zip解压
zip解压,如果压缩包里包含文件夹,则也会解压到对应的文件夹。
/**
* 解压zip文件
* @param sourcePath
* @param targetPath
*/
public static void unZip(String sourcePath,String targetPath){
File targetFile = new File(targetPath);
// 如果目录不存在,则创建
if(!targetFile.exists()){
targetFile.mkdirs();
}
try(ZipFile zipFile = new ZipFile(new File(sourcePath))) {
Enumeration enumeration = zipFile.entries();
while(enumeration.hasMoreElements()){
ZipEntry entry = (ZipEntry) enumeration.nextElement();
String name = entry.getName();
if(entry.isDirectory()){
continue;
}
try(BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry))){
// 需要判断文件所在的目录是否存在,处理压缩包里面有文件夹的情况
String outName = targetPath + "/" + name;
File outFile = new File(outName);
File tempFile = new File(outName.substring(0,outName.lastIndexOf("/")));
if (!tempFile.exists()){
tempFile.mkdirs();
}
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile))){
int len;
byte[] buffer = new byte[1024];
while((len = inputStream.read(buffer)) > 0){
outputStream.write(buffer,0,len);
}
}
}
}
} catch (Exception e){
log.error("unzip exception", e);
}
}
二、tar技术
在linux系统,我们最常见的是tar.gz压缩包,tar技术类似于gz技术,可以理解为将文件进行打包,tar解压缩需要引入org.apache.ant包。
tar压缩
tar压缩,就是可以将文件进行压缩。
/**
* tar压缩
* @param sourcePath
* @param targetPath
*/
public static void tarFile(String sourcePath,String targetPath){
try(TarOutputStream tarOutputStream = new TarOutputStream(new FileOutputStream(new File(targetPath))) ){
tarOperation(tarOutputStream,new File(sourcePath),"");
}catch (Exception e){
log.error("tarFile exception", e);
}
}
/**
* tar压缩具体操作
* @param tarOutputStream
* @param file
* @param path
*/
private static void tarOperation(TarOutputStream tarOutputStream, File file, String path){
// 如果是目录,则递归进行处理
if(file.isDirectory()) {
File[] files = file.listFiles();
for (File tempFile : files) {
tarOperation(tarOutputStream, tempFile,path + "/" + tempFile.getName());
}
}
else{
// 如果是单个文件,再进行压缩
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
//TarEntry entry = new TarEntry(path);
// 如果不设置size,会出现request to write '1024' bytes exceeds size in header of '0' bytes for entry错误
//entry.setSize(file.length());
TarEntry entry = new TarEntry(file);
tarOutputStream.putNextEntry(entry);
int len;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) > 0) {
tarOutputStream.write(buffer, 0, len);
}
tarOutputStream.closeEntry();
}catch (Exception e){
log.error("zipOperation exception", e);
}
}
}
tar解压
tar解压
/**
* tar解压
* @param sourcePath
* @param targetPath
*/
public static void unTarFile( String sourcePath,String targetPath){
File targetFile = new File(targetPath);
// 如果目录不存在,则创建
if(!targetFile.exists()){
targetFile.mkdirs();
}
try(TarInputStream tarInputStream = new TarInputStream(new FileInputStream(new File(sourcePath)))){
TarEntry entry = null;
while ((entry = tarInputStream.getNextEntry()) != null){
if(entry.isDirectory()){
continue;
}
String name = targetPath + "/" + entry.getName();
// 需要判断文件所在的目录是否存在,处理压缩包里面有文件夹的情况
File tempFile = new File(name.substring(0,name.lastIndexOf("/")));
if (!tempFile.exists()){
tempFile.mkdirs();
}
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(name)))){
int len;
byte[] buffer = new byte[1024];
while((len = tarInputStream.read(buffer)) > 0){
outputStream.write(buffer,0,len);
}
}
}
}catch (Exception e){
log.error("unTarFile exception", e);
}
}
三、gz技术
gz技术,可以理解为将文件进行压缩,gz技术只能对单一文件进行压缩,不能同时压缩多文件,所以我们一般会把多个文件打包成一个tar包,然后再使用gz进行压缩。
gz压缩
public static void gzFile(String sourcePath,String targetPath){
try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(sourcePath)));
GZIPOutputStream outputStream = new GZIPOutputStream(new FileOutputStream(new File(targetPath)))){
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) > 0){
outputStream.write(buffer,0,len);
}
}catch (Exception e){
log.error("gzFile exception",e);
}
}
gz解压
/**
* 解压tar.gz文件
* @param sourcePath
* @param targetPath
*/
public static void unGzFile(String sourcePath,String targetPath){
String name = sourcePath.substring(sourcePath.lastIndexOf("/") + 1,sourcePath.length());
String tarName = targetPath + name.substring(0,name.lastIndexOf("."));
try(GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(new File(sourcePath)));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(tarName)))){
byte[] buffer = new byte[1024];
int len;
while((len = inputStream.read(buffer)) > 0){
outputStream.write(buffer,0,len);
}
outputStream.flush();
unTarFile(tarName,targetPath);
}catch (Exception e){
log.error("unGzFile exception",e);
}
}
四、测试
使用junti来进行测试
@Test
public void zipTest(){
// zip压缩
/*String sourcePath = "/data/jsp/springTest/logs/";
String targePath = "/data/jsp/springTest/logs.zip";
ZipUtil.createZip(sourcePath,targePath);*/
// zip解压
/*String sourcePath = "/data/jsp/springTest/logs.zip";
String targetPath = "/data/jsp/springTest/uzlogs";
ZipUtil.unZip(sourcePath,targetPath);*/
// tar压缩
/*String sourcePath = "/data/jsp/springTest/logs/";
String targetPath = "/data/jsp/springTest/logs.tar";
TarGzUtil.tarFile(sourcePath,targetPath);*/
// tara解压
/*String sourcePath = "/data/jsp/springTest/logs.tar";
String targetPath = "/data/jsp/springTest/untarLogs/";
TarGzUtil.unTarFile(sourcePath,targetPath);*/
// gz压缩
/*String sourcePath = "/data/jsp/springTest/logs.tar";
String targetPath = "/data/jsp/springTest/logs.tar.gz";
TarGzUtil.gzFile(sourcePath,targetPath);*/
// gz解压
String sourcePath = "/data/jsp/springTest/logs.tar.gz";
String targetPath = "/data/jsp/";
TarGzUtil.unGzFile(sourcePath,targetPath);
}