Hi,
Does commons-compress support appending to compressed files?
E.g. if I start my stream as following:
FileOutputStream out2 = new FileOutputStream(output2, true); //append
if file exists
CompressorOutputStream cos2 = new
CompressorStreamFactory().createCompressorOutputStream("bzip2", out2);
I expect to be able to append to a .bz2 file.
Below please find BZip2TestCase with an extra failing test, that
suggests that append isn't supported.
Are there plans to support append?
if I use command line bzcat/bunzip2 I can see all lines I put into an
archine, however CompressorOutputStream only reads to the end of the
first write (appended data is ignored)
Sincerely,
Alexander
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.compress.compressors;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.Exception;
import java.lang.System;
import org.apache.commons.compress.AbstractTestCase;
import org.apache.commons.compress.utils.IOUtils;
public final class BZip2TestCase extends AbstractTestCase {
public void testBzipCreation() throws Exception {
File output = null;
final File input = getFile("test.txt");
{
output = new File(dir, "test.txt.bz2");
final OutputStream out = new FileOutputStream(output);
final CompressorOutputStream cos = new
CompressorStreamFactory().createCompressorOutputStream("bzip2", out);
FileInputStream in = new FileInputStream(input);
IOUtils.copy(in, cos);
cos.close();
in.close();
}
final File decompressed = new File(dir, "decompressed.txt");
{
final File toDecompress = output;
final InputStream is = new FileInputStream(toDecompress);
final CompressorInputStream in =
new
CompressorStreamFactory().createCompressorInputStream("bzip2", is);
FileOutputStream os = new FileOutputStream(decompressed);
IOUtils.copy(in, os);
is.close();
os.close();
}
assertEquals(input.length(),decompressed.length());
}
public void testBzip2Unarchive() throws Exception {
final File input = getFile("bla.txt.bz2");
final File output = new File(dir, "bla.txt");
final InputStream is = new FileInputStream(input);
final CompressorInputStream in = new
CompressorStreamFactory().createCompressorInputStream("bzip2", is);
FileOutputStream os = new FileOutputStream(output);
IOUtils.copy(in, os);
is.close();
os.close();
}
public void testBzipAppendRounReadWrite() throws Exception {
// read text file, append contents to .bz2 twice!
final File input = getFile("test.txt");
final InputStream in = new FileInputStream(input);
// write text.txt to bla.txt.bz2
final File output = new File(dir, "bla.txt.bz2");
FileOutputStream out = new FileOutputStream(output);
final CompressorOutputStream cos = new
CompressorStreamFactory().createCompressorOutputStream("bzip2", out);
IOUtils.copy(in, cos);
in.close();
cos.close();
File decompressed = readBzip(output);
long firstWrite= output.length();
assertEquals(input.length(), decompressed.length());
// now, append test.txt to bla.txt.bz2
final InputStream in2 = new FileInputStream(input);
final File output2 = new File(dir, "bla.txt.bz2");
FileOutputStream out2 = new FileOutputStream(output2, true);
//append if file exists
final CompressorOutputStream cos2 = new
CompressorStreamFactory().createCompressorOutputStream("bzip2", out2);
IOUtils.copy(in2, cos2);
in.close();
cos2.close();
File decompressed2 = readBzip(output);
long secondWrite= output.length();
System.out.println("first write: " + firstWrite + ", second
write: " + secondWrite);
System.out.println("input: " + input.length() +",
decompressed: " + decompressed2.length());
assertEquals(firstWrite * 2, secondWrite);
assertEquals(input.length(), decompressed2.length());
}
private File readBzip(File output) throws Exception {
final File decompressed = new File(dir, "decompressed.txt");
final File toDecompress = output;
final InputStream is = new FileInputStream(toDecompress);
final CompressorInputStream in =
new
CompressorStreamFactory().createCompressorInputStream("bzip2", is);
FileOutputStream os = new FileOutputStream(decompressed);
IOUtils.copy(in, os);
is.close();
os.close();
return decompressed;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]