org.tukaani.xz.XZOutputStream Java Examples

The following examples show how to use org.tukaani.xz.XZOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LZMA2.java    From OSPREY3 with GNU General Public License v2.0 6 votes vote down vote up
public static byte[] compress(String text) {
	ByteArrayOutputStream buf = new ByteArrayOutputStream();
	try (XZOutputStream out = new XZOutputStream(buf, options)) {

		// encode the text as bytes
		byte[] bytes = text.getBytes(charset);

		// encode the uncompressed length as characters,
		// so if we try to view the compressed file with standard desktop tools,
		// the decompressed blob will look like a text file
		// 12 characters should be more than enough for an int in decimal encoding
		byte[] lenbuf = String.format("%-12d", bytes.length).getBytes(charset);
		if (lenbuf.length != 12) {
			throw new Error("length encoded to unexpected size: " + lenbuf.length);
		}
		out.write(lenbuf);

		// write the payload
		out.write(bytes);

	} catch (IOException ex) {
		throw new RuntimeException("can't compress", ex);
	}
	return buf.toByteArray();
}
 
Example #2
Source File: XzStep.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException {
  boolean deleteSource = false;
  try (InputStream in = filesystem.newFileInputStream(sourceFile);
      OutputStream out = filesystem.newFileOutputStream(destinationFile);
      XZOutputStream xzOut = new XZOutputStream(out, new LZMA2Options(compressionLevel), check)) {
    XzMemorySemaphore.acquireMemory(compressionLevel);
    ByteStreams.copy(in, xzOut);
    xzOut.finish();
    deleteSource = !keep;
  } finally {
    XzMemorySemaphore.releaseMemory(compressionLevel);
  }
  if (deleteSource) {
    filesystem.deleteFileAtPath(sourceFile);
  }
  return StepExecutionResults.SUCCESS;
}
 
Example #3
Source File: HeapAnalysisModule.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Path compress(Path file, LongConsumer progressHandler) throws IOException {
    Path compressedFile = file.getParent().resolve(file.getFileName().toString() + ".xz");
    try (InputStream in = Files.newInputStream(file)) {
        try (OutputStream out = Files.newOutputStream(compressedFile)) {
            try (XZOutputStream compressionOut = new XZOutputStream(out, new LZMA2Options())) {
                copy(in, compressionOut, progressHandler);
            }
        }
    }
    return compressedFile;
}
 
Example #4
Source File: StandardCompressXz.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
//		InputStream fis;
		//File file = new File(DEFAULT_DIR, TEMP_FILE);
		// This works both within Eclipse project and in runnable JAR
        //InputStream fis = StandardCompressXz.class.getResourceAsStream("SurfaceMarsMap.dat");
		// This works both within Eclipse project and in runnable JAR
        //InputStream fis = this.getClass().getClassLoader().getResourceAsStream("/map/SurfaceMarsMap.dat");
        
        //fis = this.getClass().getClassLoader().getResourceAsStream("examples/resources/verdana.ttf");
        
//        fis = StandardCompressXz.class.getClassLoader().getResourceAsStream("SurfaceMarsMap.dat.7z");
        
		FileInputStream inFile = new FileInputStream(StandardCompressXz.class.getClassLoader().getResource("/map/SurfaceMarsMap.dat").toExternalForm());//"SurfaceMarsMap.dat");
		
		FileOutputStream outfile = new FileOutputStream("/map/SurfaceMarsMap.xz");	
		
		LZMA2Options options = new LZMA2Options();

		options.setPreset(7); // play with this number: 6 is default but 7 works better for mid sized archives ( > 8mb)

		XZOutputStream out = new XZOutputStream(outfile, options);

		byte[] buf = new byte[8192];
		int size;
		while ((size = inFile.read(buf)) != -1)
		   out.write(buf, 0, size);

		out.finish();
	}
 
Example #5
Source File: XZCompressionFilterTest.java    From kieker with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link kieker.monitoring.writer.compression.XZCompressionFilter#chainOutputStream(java.io.OutputStream, java.nio.file.Path)}.
 */
@Test
public void testChainOutputStream() {
	final String inputStr = "Hello World";
	final byte[] inputData = inputStr.getBytes(Charset.defaultCharset());
	final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	// Stream replicating constructor in test
	final ByteArrayOutputStream byteArrayOutputStreamR = new ByteArrayOutputStream();
	final Configuration configuration = ConfigurationFactory.createDefaultConfiguration();
	final XZCompressionFilter unit = new XZCompressionFilter(configuration);
	final Path path = Paths.get("");

	try {
		// Passing inflated output stream
		final OutputStream value = unit.chainOutputStream(byteArrayOutputStream, path);
		// Writing byte array to stream
		value.write(inputData);
		// Closing stream
		value.close();

		// Replicating method to be tested
		final FilterOptions filterOptions = new LZMA2Options(LZMA2Options.PRESET_MAX);
		final XZOutputStream xzzip = new XZOutputStream(byteArrayOutputStreamR, filterOptions);
		xzzip.write(inputData);
		xzzip.close();

		// Checking if byteArrayOutputStreamR is equal to byteArrayOutputStream
		Assert.assertArrayEquals("Expected result does not match with actual result",
				byteArrayOutputStreamR.toByteArray(), byteArrayOutputStream.toByteArray());

	} catch (final IOException e) {
		e.printStackTrace();
		Assert.fail(e.getMessage());
	}
}
 
Example #6
Source File: LZMA2.java    From OSPREY3 with GNU General Public License v2.0 5 votes vote down vote up
public static byte[] compress(byte[] bytes) {
	ByteArrayOutputStream buf = new ByteArrayOutputStream();
	try (DataOutputStream out = new DataOutputStream(new XZOutputStream(buf, options))) {

		// encode the uncompressed length as a simple int
		out.writeInt(bytes.length);

		// write the payload
		out.write(bytes);

	} catch (IOException ex) {
		throw new RuntimeException("can't compress", ex);
	}
	return buf.toByteArray();
}
 
Example #7
Source File: WriteFilmlistJson.java    From MLib with GNU General Public License v3.0 5 votes vote down vote up
private void compressFile(String inputName, String outputName) throws IOException {
    try (InputStream input = new FileInputStream(inputName);
         FileOutputStream fos = new FileOutputStream(outputName);
         final OutputStream output = new XZOutputStream(fos, new LZMA2Options());
         final ReadableByteChannel inputChannel = Channels.newChannel(input);
         final WritableByteChannel outputChannel = Channels.newChannel(output)) {

        fastChannelCopy(inputChannel, outputChannel);
    } catch (IOException ignored) {
    }
}
 
Example #8
Source File: XZCompressionFilter.java    From kieker with Apache License 2.0 4 votes vote down vote up
@Override
public OutputStream chainOutputStream(final OutputStream outputStream, final Path fileName) throws IOException {
	final FilterOptions filterOptions = new LZMA2Options(LZMA2Options.PRESET_MAX);
	return new XZOutputStream(outputStream, filterOptions);
}