ning/compress — Issue #12: Command-line tool out of memory
Author: nodarret
Created: 2011-10-10 10:57:05+00:00
State: open

For big files better way is use Input/Output Streams. Very useful will be to add parametr sending results into standard output.

Example:
-d parameter: decompress to file
-c parameter: compress to file
-o parameter: decompress to output

Example Java code:

```java
void process(String[] args) throws IOException
{
if (args.length == 2) {
String oper = args[0];
boolean toSystemOutput = false;
boolean compress = "-c".equals(oper);
if ("-o".equals(oper)) {
toSystemOutput = true;
}
if (compress || "-d".equals(oper) || "-o".equals(oper)) {
String filename = args[1];
File src = new File(filename);
if (!src.exists()) {
System.err.println("File '"+filename+"' does not exist.");
System.exit(1);
}
if (!compress && !filename.endsWith(SUFFIX)) {
System.err.println("File '"+filename+"' does end with expected suffix ('"+SUFFIX+"', won't decompress.");
System.exit(1);
}

InputStream is;
OutputStream out;

if (!compress) {
is = new LZFFileInputStream(src);
if (toSystemOutput) {
out = System.out;
} else {
out = new FileOutputStream(new File(filename.substring(0, filename.length() - SUFFIX.length())));
}
} else {
is = new FileInputStream(src);
out = new LZFFileOutputStream(new File(filename+SUFFIX));
}

byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
out.close();

return;
}
}
System.err.println("Usage: java "+getClass().getName()+" -c/-d/-o file");
System.exit(1);
}
```