在 java 中使用 GZIP 压缩文件
compress file with GZIP in java
我尝试用缓冲输入流读取一个 txt 文件,并用 GZIP 压缩它,它成功了。但是,当我尝试使用 zip 解压压缩文件时,该文件似乎是不可读的二进制格式,我该如何解决这个问题?以下是我的代码:
public static void main(String[] args) {
compressWithGZIP(SAVE_PATH2, SAVE_PATH3);
//uncompressWithGZIP(SAVE_PATH3 + "compressed.gz", SAVE_PATH4);
}
private static void uncompressWithGZIP(String oripath, String outputPath) {
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
try {
bi = new BufferedInputStream(new GZIPInputStream(
new FileInputStream(oripath)));
bo = new BufferedOutputStream(new FileOutputStream(outputPath));
int c;
while ((c = bi.read()) != -1) {
bo.write(c);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (bi != null) {
bi.close();
}
if (bo != null) {
bo.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void compressWithGZIP(String filePath, String outputPath) {
if (outputPath == null || outputPath.isEmpty()
|| !outputPath.endsWith(".gz")) {
outputPath += "compressed.gz";
}
BufferedReader br = null;
BufferedOutputStream bo = null;
try {
br = new BufferedReader(new FileReader(filePath));
bo = new BufferedOutputStream(new GZIPOutputStream(
new FileOutputStream(outputPath)));
int c;
while ((c = br.read()) != -1) {
bo.write(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (bo != null) {
bo.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
经典错误。
做。不是。曾经。采用。答:READER。到。读。二进制。数据..
A Reader
使用字符解码过程将从文件中读取的数据解释为潜在字符。 是为什么Java定义了Reader与InputStream和Writer与OutputStream的原因。
如果您处理二进制数据,请使用 InputStream 和 OutputStream。 从不 Reader 或作家。
换句话说你的问题在这里:
br = new BufferedReader(new FileReader(filePath));
bo = new BufferedOutputStream(new GZIPOutputStream(
new FileOutputStream(outputPath)));
使用 InputStream
而不是 Reader
从源文件中读取。
我尝试用缓冲输入流读取一个 txt 文件,并用 GZIP 压缩它,它成功了。但是,当我尝试使用 zip 解压压缩文件时,该文件似乎是不可读的二进制格式,我该如何解决这个问题?以下是我的代码:
public static void main(String[] args) {
compressWithGZIP(SAVE_PATH2, SAVE_PATH3);
//uncompressWithGZIP(SAVE_PATH3 + "compressed.gz", SAVE_PATH4);
}
private static void uncompressWithGZIP(String oripath, String outputPath) {
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
try {
bi = new BufferedInputStream(new GZIPInputStream(
new FileInputStream(oripath)));
bo = new BufferedOutputStream(new FileOutputStream(outputPath));
int c;
while ((c = bi.read()) != -1) {
bo.write(c);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (bi != null) {
bi.close();
}
if (bo != null) {
bo.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void compressWithGZIP(String filePath, String outputPath) {
if (outputPath == null || outputPath.isEmpty()
|| !outputPath.endsWith(".gz")) {
outputPath += "compressed.gz";
}
BufferedReader br = null;
BufferedOutputStream bo = null;
try {
br = new BufferedReader(new FileReader(filePath));
bo = new BufferedOutputStream(new GZIPOutputStream(
new FileOutputStream(outputPath)));
int c;
while ((c = br.read()) != -1) {
bo.write(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (bo != null) {
bo.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
经典错误。
做。不是。曾经。采用。答:READER。到。读。二进制。数据..
A Reader
使用字符解码过程将从文件中读取的数据解释为潜在字符。 是为什么Java定义了Reader与InputStream和Writer与OutputStream的原因。
如果您处理二进制数据,请使用 InputStream 和 OutputStream。 从不 Reader 或作家。
换句话说你的问题在这里:
br = new BufferedReader(new FileReader(filePath));
bo = new BufferedOutputStream(new GZIPOutputStream(
new FileOutputStream(outputPath)));
使用 InputStream
而不是 Reader
从源文件中读取。