correctly closed streams when extracting a gzip archive

Former-commit-id: 0a0c917cc3e4c4a004823fba9b7f8ab53f90d557
This commit is contained in:
Jeremy Long
2014-11-16 06:21:02 -05:00
parent 127eafc9b3
commit 299350f655

View File

@@ -260,26 +260,38 @@ public class DownloadTask implements Callable<Future<ProcessTask>> {
* @throws IOException thrown if there is an error extracting the file. * @throws IOException thrown if there is an error extracting the file.
*/ */
private void extractGzip(File file) throws FileNotFoundException, IOException { private void extractGzip(File file) throws FileNotFoundException, IOException {
String originalPath = file.getPath(); final String originalPath = file.getPath();
File gzip = new File(originalPath + ".gz"); File gzip = new File(originalPath + ".gz");
if (gzip.isFile()) { if (gzip.isFile()) {
gzip.delete(); gzip.delete();
} }
file.renameTo(gzip); if (!file.renameTo(gzip)) {
file = new File(originalPath); throw new IOException("Unable to rename '" + file.getPath() + "'");
}
byte[] buffer = new byte[4096]; final File newfile = new File(originalPath);
GZIPInputStream cin = new GZIPInputStream(new FileInputStream(gzip)); final byte[] buffer = new byte[4096];
FileOutputStream out = new FileOutputStream(file); GZIPInputStream cin = null;
FileOutputStream out = null;
int len; try {
while ((len = cin.read(buffer)) > 0) { cin = new GZIPInputStream(new FileInputStream(gzip));
out.write(buffer, 0, len); out = new FileOutputStream(newfile);
int len;
while ((len = cin.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} finally {
if (cin != null) {
cin.close();
}
if (out != null) {
out.close();
}
if (gzip.isFile()) {
FileUtils.delete(gzip);
}
} }
cin.close();
out.close();
FileUtils.delete(gzip);
} }
} }