Java源码示例:org.tukaani.xz.XZOutputStream

示例1
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();
}
 
示例2
@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;
}
 
示例3
@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;
}
 
示例4
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();
	}
 
示例5
/**
 * 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());
	}
}
 
示例6
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();
}
 
示例7
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) {
    }
}
 
示例8
@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);
}