Java 7 个压缩文件
Java 7 Deflating Files
我有一段使用deflate
算法压缩文件的代码:
public static File compressOld(File rawFile) throws IOException
{
File compressed = new File(rawFile.getCanonicalPath().split("\.")[0]
+ "_compressed." + rawFile.getName().split("\.")[1]);
InputStream inputStream = new FileInputStream(rawFile);
OutputStream compressedWriter = new DeflaterOutputStream(new FileOutputStream(compressed));
byte[] buffer = new byte[1000];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
compressedWriter.write(buffer, 0, length);
}
inputStream.close();
compressedWriter.close();
return compressed;
}
但是,我对OutputStream
copying loop since it's the "outdated" way of writing to streams. Instead, I want to use a Java 7
API method such as Files.copy
不满意:
public static File compressNew(File rawFile) throws IOException
{
File compressed = new File(rawFile.getCanonicalPath().split("\.")[0]
+ "_compressed." + rawFile.getName().split("\.")[1]);
OutputStream compressedWriter = new DeflaterOutputStream(new FileOutputStream(compressed));
Files.copy(compressed.toPath(), compressedWriter);
compressedWriter.close();
return compressed;
}
但是后一种方法不能正常工作,压缩文件乱七八糟,只复制了几个字节。怎么会?
我主要看到两个问题
- 您从目标而不是源复制。我认为复制必须更改为
Files.copy(rawFile.toPath(), compressedWriter);
.
-
copy
的 Java 文档说: "Note that if the given output stream is Flushable then its flush method may need to invoked after this method completes so as to flush any buffered output." 所以,你必须在 [=11] 之后调用 OutputStream
的 flush
方法=].
另外还有一点。 Java副本的文档说:
It is strongly recommended that the output stream be promptly closed if an I/O error occurs.
您可以关闭 finally
块中的 OutputStream
以确保它在出现错误时发生。另一种可能性是使用 Java 7.
中引入的 try with resources
我有一段使用deflate
算法压缩文件的代码:
public static File compressOld(File rawFile) throws IOException
{
File compressed = new File(rawFile.getCanonicalPath().split("\.")[0]
+ "_compressed." + rawFile.getName().split("\.")[1]);
InputStream inputStream = new FileInputStream(rawFile);
OutputStream compressedWriter = new DeflaterOutputStream(new FileOutputStream(compressed));
byte[] buffer = new byte[1000];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
compressedWriter.write(buffer, 0, length);
}
inputStream.close();
compressedWriter.close();
return compressed;
}
但是,我对OutputStream
copying loop since it's the "outdated" way of writing to streams. Instead, I want to use a Java 7
API method such as Files.copy
不满意:
public static File compressNew(File rawFile) throws IOException
{
File compressed = new File(rawFile.getCanonicalPath().split("\.")[0]
+ "_compressed." + rawFile.getName().split("\.")[1]);
OutputStream compressedWriter = new DeflaterOutputStream(new FileOutputStream(compressed));
Files.copy(compressed.toPath(), compressedWriter);
compressedWriter.close();
return compressed;
}
但是后一种方法不能正常工作,压缩文件乱七八糟,只复制了几个字节。怎么会?
我主要看到两个问题
- 您从目标而不是源复制。我认为复制必须更改为
Files.copy(rawFile.toPath(), compressedWriter);
. -
copy
的 Java 文档说: "Note that if the given output stream is Flushable then its flush method may need to invoked after this method completes so as to flush any buffered output." 所以,你必须在 [=11] 之后调用OutputStream
的flush
方法=].
另外还有一点。 Java副本的文档说:
It is strongly recommended that the output stream be promptly closed if an I/O error occurs.
您可以关闭 finally
块中的 OutputStream
以确保它在出现错误时发生。另一种可能性是使用 Java 7.