java代码实现压缩文件.gz格式,解压后无后缀名问题
java代碼實現壓縮文件.gz格式,解壓后無后綴名問題
package com.ctid.cps.util.gzipUtil;
import com.ctid.util.file.FileUtil;
import com.ctid.util.file.GZip;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
-
GZIP工具
-
@author
*/
public class GzipUtils {public static final int BUFFER = 1024;
public static final String EXT = “.gz”;/**
-
數據壓縮
-
@param data
-
@return
-
@throws Exception
*/
public static byte[] compress(byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();// 壓縮
compress(bais, baos);byte[] output = baos.toByteArray();
baos.flush();
baos.close();bais.close();
return output;
}
/**
- 文件壓縮
- @param file
- @throws Exception
*/
public static void compress(File file) throws Exception {
compress(file, true);
}
/**
-
文件壓縮
-
@param file
-
@param delete 是否刪除原始文件
-
@throws Exception
*/
public static void compress(File file, boolean delete) throws Exception {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);compress(fis, fos);
fis.close();
fos.flush();
fos.close();if (delete) {
file.delete();
}
}
/**
-
數據壓縮
-
@param is
-
@param os
-
@throws Exception
*/
public static void compress(InputStream is, OutputStream os)
throws Exception {GZIPOutputStream gos = new GZIPOutputStream(os);
int count;
byte data[] = new byte[BUFFER];
while ((count = is.read(data, 0, BUFFER)) != -1) {
gos.write(data, 0, count);
}gos.finish();
gos.flush();
gos.close();
}
/**
- 文件壓縮
- @param path
- @throws Exception
*/
public static void compress(String path) throws Exception {
compress(path, true);
}
/**
- 文件壓縮
- @param path
- @param delete 是否刪除原始文件
- @throws Exception
*/
public static void compress(String path, boolean delete) throws Exception {
File file = new File(path);
compress(file, delete);
}
/**
- 文件壓縮
- @param
- @param
- @throws Exception
*/
public static void compress(String inputFileName, String outputFileName)
throws Exception {
FileInputStream inputFile = new FileInputStream(inputFileName);
FileOutputStream outputFile = new FileOutputStream(outputFileName);
compress(inputFile, outputFile);
inputFile.close();
outputFile.flush();
outputFile.close();
}
/**
-
數據解壓縮
-
@param data
-
@return
-
@throws Exception
*/
public static byte[] decompress(byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();// 解壓縮
decompress(bais, baos);
data = baos.toByteArray();
baos.flush();
baos.close();bais.close();
return data;
}
/**
- 文件解壓縮
- @param file
- @throws Exception
*/
public static void decompress(File file) throws Exception {
decompress(file, true, null);
}
/**
-
文件解壓縮
-
@param file 需要解壓的文件
-
@param delete 是否刪除原始文件
-
@param outPath 解壓文件的輸出路徑
-
@throws Exception
*/
public static void decompress(File file, boolean delete, String outPath)
throws Exception {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = null;
if (outPath == null || outPath == “”) {
fos = new FileOutputStream(file.getPath().replace(EXT, “”));
} else {
File files = new File(outPath);
//判斷文件是否存在,不存在,則創建
FileUtil.mkDir(files);//此處調用了遞歸創建文件夾,沒有寫出,網上很多
//文件輸出流參數中,需要指定文件解壓后的文件名,這里,用文件的原名稱
fos = new FileOutputStream(outPath + File.separator
+ file.getName().replace(EXT, “”));
}decompress(fis, fos);
fis.close();
fos.flush();
fos.close();if (delete) {
file.delete();
}
}
/**
- 文件解壓縮
- @param
- @param
- @throws Exception
*/
public static void decompress(String inputFileName, String outputFileName)
throws Exception {
FileInputStream inputFile = new FileInputStream(inputFileName);
FileOutputStream outputFile = new FileOutputStream(outputFileName);
decompress(inputFile, outputFile);
inputFile.close();
outputFile.flush();
outputFile.close();
}
/**
-
數據解壓縮
-
@param is
-
@param os
-
@throws Exception
*/
public static void decompress(InputStream is, OutputStream os)
throws Exception {
GZIPInputStream gis = new GZIPInputStream(is);
//GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(is));
int count;
byte data[] = new byte[BUFFER];
while ((count = gis.read(data, 0, BUFFER)) != -1) {
os.write(data, 0, count);
}gis.close();
}
/**
- 文件解壓縮
- @param path
- @throws Exception
*/
public static void decompress(String path) throws Exception {
decompress(path, true, null);
}
/**
- 文件解壓縮(解壓單個文件)
- @param path 需要解壓的文件路徑(包含文件名稱)
- @param delete 是否刪除原始文件(true:刪除;false:保留)
- @param outPath 解壓后文件的輸出路徑,如果該參數的值為 null,則輸出解壓文件到當前文件夾
- @throws Exception
*/
public static void decompress(String path, boolean delete, String outPath)
throws Exception {
File file = new File(path);
decompress(file, delete, outPath);
}
//測試,通過遍歷,解壓一個文件夾中的所有文件
/* public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();// 記錄開始時間
String path = “D:\compreFile\test2”;
File file = new File(path);
String files[] = file.list();
System.out.println(“共 " + files.length + " 個文件”);
Thread.sleep(2000);
int num = 0;
List list = new ArrayList();
for (int x = 0; x < files.length; x++) {
try {
//調用解壓方法
decompress(path + “\” + files[x], false, “D:\compreFile\test2”);
//GZip.unpackTarGZ(file,“D:\compreFile\test2”);
} catch (Exception e) {
list.add(files[x]);
continue;
}
++num;
System.out.println(“第 " + num + " 個文件.tar.gz解壓成功!!”);
}
// String tarPath = “D:\compreFile\test2\”;
// File fileTar = new File(tarPath);
// String filesTar[] = fileTar.list();
// int nber = 0;
// for (int j = 0; j < filesTar.length; j++) {
// try {
// //調用解壓方法
// tarToFile.uncompress(tarPath + filesTar[j], “D:\compreFile\test”);
// } catch (Exception e) {
// list.add(files[j]);
// continue;
// }
// ++nber;
// System.out.println(“第 " + nber + " 個文件.tar解壓成功!!”);
// }
for(int x=0;x<list.size();x++){
System.out.println(list.get(x).toString());
}
System.out.println("問題文件 " + list.size() + " 個 ");public static void main(String[] args) throws Exception {
TarToFile tarToFile = new TarToFile();
long startTime = System.currentTimeMillis();// 記錄開始時間
String path = “D:\JAVA壓縮解壓\”;
File file = new File(path);
String files[] = file.list();
System.out.println(“共 " + files.length + " 個文件”);
Thread.sleep(2000);
int num = 0;
List list = new ArrayList();
for (int x = 0; x < files.length; x++) {
try {
//調用壓縮方法
compress(path + files[x], “D:\compreFile\test2\ceshi1” + num + “.gz”);
System.out.println(path + files[x]);
} catch (Exception e) {
System.out.println(e);
list.add(files[x]);
continue;
}
++num;
System.out.println(“第 " + num + " 個文件壓縮成功!!”);
}
System.out.println("問題文件 " + list.size() + " 個 ");
long endTime = System.currentTimeMillis();// 記錄結束時間
float excTime = (float) (endTime - startTime) / 1000;
System.out.println(“執行時間:” + excTime + “s”);
System.out.println(“完成!!”);
}
} -
這是我的代碼,找了一個小時的問題,為什么解壓以后無后綴,最終是發現壓縮的時候在.gz 前面加上文件后綴,比如你要壓縮一個 .java文件
( //調用壓縮方法的時候
compress(path + files[x], “D:\compreFile\test2\ceshi1” + num + “.java.gz”);)//把文件后綴得加上去
總結
以上是生活随笔為你收集整理的java代码实现压缩文件.gz格式,解压后无后缀名问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用selenium + pytest
- 下一篇: STM32F4 HAL 库开发报错:Ha