| | |
| | | @Override |
| | | public boolean execute() |
| | | { |
| | | FileInputStream fis = null; |
| | | GZIPOutputStream gzip = null; |
| | | boolean inputStreamOpen = false; |
| | | boolean outputStreamOpen = false; |
| | | |
| | | try |
| | | { |
| | | if(!originalFile.exists()) |
| | |
| | | return false; |
| | | } |
| | | |
| | | fis = new FileInputStream(originalFile); |
| | | inputStreamOpen = true; |
| | | FileOutputStream fos = new FileOutputStream(newFile); |
| | | gzip = new GZIPOutputStream(fos); |
| | | outputStreamOpen = true; |
| | | |
| | | byte[] buf = new byte[8192]; |
| | | int n; |
| | | |
| | | while((n = fis.read(buf)) != -1) |
| | | try (FileInputStream fis = new FileInputStream(originalFile); |
| | | FileOutputStream fos = new FileOutputStream(newFile); |
| | | GZIPOutputStream gzip = new GZIPOutputStream(fos);) |
| | | { |
| | | gzip.write(buf, 0, n); |
| | | byte[] buf = new byte[8192]; |
| | | int n; |
| | | while ((n = fis.read(buf)) != -1) |
| | | { |
| | | gzip.write(buf, 0, n); |
| | | } |
| | | } |
| | | |
| | | gzip.close(); |
| | | outputStreamOpen = false; |
| | | fis.close(); |
| | | inputStreamOpen = false; |
| | | |
| | | if(deleteOriginal && !originalFile.delete()) |
| | | { |
| | | System.err.println("Cannot delete original file:" + originalFile); |
| | |
| | | } catch(IOException ioe) |
| | | { |
| | | logger.traceException(ioe); |
| | | if (inputStreamOpen) |
| | | { |
| | | try |
| | | { |
| | | fis.close(); |
| | | } |
| | | catch (Exception fe) |
| | | { |
| | | logger.traceException(fe); |
| | | // Cannot do much. Ignore. |
| | | } |
| | | } |
| | | if (outputStreamOpen) |
| | | { |
| | | try |
| | | { |
| | | gzip.close(); |
| | | } |
| | | catch (Exception ge) |
| | | { |
| | | logger.traceException(ge); |
| | | // Cannot do much. Ignore. |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | |