org.tukaani.xz.XZInputStream Java Examples

The following examples show how to use org.tukaani.xz.XZInputStream. 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: XZPackLibraryDownloadHandler.java    From JMCCC with MIT License 6 votes vote down vote up
@Override
public DownloadTask<Void> createDownloadTask(File target, Library library, URI libraryUri) {
	return new MemoryDownloadTask(libraryUri).andThen(new ResultProcessor<byte[], byte[]>() {

		@Override
		public byte[] process(byte[] data) throws IOException {
			try (XZInputStream in = new XZInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream();) {
				byte[] buffer = new byte[8192];
				int read;
				while ((read = in.read(buffer)) != -1) {
					out.write(buffer, 0, read);
				}
				return out.toByteArray();
			}
		}

	}).andThen(new PackProcessor(target));
}
 
Example #2
Source File: LZMA2.java    From OSPREY3 with GNU General Public License v2.0 6 votes vote down vote up
public static byte[] decompressBytes(byte[] bytes) {
	try (ByteArrayInputStream bin = new ByteArrayInputStream(bytes)) {
		try (DataInputStream in = new DataInputStream(new XZInputStream(bin))) {

			// read the length, encoded as a simple int
			int length = in.readInt();

			// read the payload
			byte[] out = new byte[length];
			in.readFully(out);

			// the docs say to try to read an extra byte, so the library can check the footers
			if (in.read() != -1) {
				throw new IOException("Expected EOF, but it wasn't");
			}

			return out;
		}
	} catch (IOException ex) {
		throw new RuntimeException("can't decompress", ex);
	}
}
 
Example #3
Source File: LZMA2.java    From OSPREY3 with GNU General Public License v2.0 6 votes vote down vote up
public static String decompressString(byte[] bytes) {
	try (ByteArrayInputStream bin = new ByteArrayInputStream(bytes)) {
		try (DataInputStream in = new DataInputStream(new XZInputStream(bin))) {

			// read the length, decoding the characters, then decoding the decimal
			byte[] lenbuf = new byte[12];
			in.readFully(lenbuf);
			int length = Integer.parseInt(new String(lenbuf, charset).trim());

			// read the payload
			byte[] out = new byte[length];
			in.readFully(out);

			// the docs say to try to read an extra byte, so the library can check the footers
			if (in.read() != -1) {
				throw new IOException("Expected EOF, but it wasn't");
			}

			return new String(out, charset);
		}
	} catch (IOException ex) {
		throw new RuntimeException("can't decompress", ex);
	}
}
 
Example #4
Source File: AndroidNdkHelper.java    From buck with Apache License 2.0 6 votes vote down vote up
public Symbols getXzsSymbols(Path apkPath, String libName, String xzsName, String metadataName)
    throws IOException, InterruptedException {
  Path xzs = unpack(apkPath, xzsName);
  Path metadata = unpack(apkPath, metadataName);
  Path lib = tmpDir.resolve(libName);
  try (BufferedReader metadataReader = new BufferedReader(new FileReader(metadata.toFile()))) {
    try (XZInputStream xzInput =
        new XZInputStream(new FileInputStream(xzs.toFile()), -1, false)) {
      String line = metadataReader.readLine();
      while (line != null) {
        String[] tokens = line.split(" ");
        File metadataFile = new File(tokens[0]);
        if (metadataFile.getName().equals(libName)) {
          break;
        }
        advanceStream(xzInput, Integer.parseInt(tokens[1]));
        line = metadataReader.readLine();
      }
      Files.copy(xzInput, lib, StandardCopyOption.REPLACE_EXISTING);
    }
  }
  return Symbols.getDynamicSymbols(executor, objdump, resolver, lib);
}
 
Example #5
Source File: FridaAgent.java    From FridaHooker with GNU General Public License v2.0 5 votes vote down vote up
public File extractLocalFrida(InputStream is, String to) throws IOException {
    File cacheFile = new File(to + File.separator + "frida-server");
    XZInputStream xzis = new XZInputStream(is);
    FileOutputStream fos = new FileOutputStream(cacheFile);
    int len;
    byte[] buffer = new byte[4096];
    while (-1 != (len = xzis.read(buffer))) {
        fos.write(buffer, 0, len);
        fos.flush();
    }
    xzis.close();
    fos.close();
    LogUtil.d(TAG, "Local frida cache file path: "+cacheFile.getAbsolutePath());
    return cacheFile;
}
 
Example #6
Source File: CompressionUtil.java    From entrada with GNU General Public License v3.0 5 votes vote down vote up
/**
 * wraps the inputstream with a decompressor based on a filename ending
 *
 * @param in The input stream to wrap with a decompressor
 * @param filename The filename from which we guess the correct decompressor
 * @param bufSize size of the read buffer to use, in bytes
 * @return the compressor stream wrapped around the inputstream. If no decompressor is found,
 *         returns the inputstream wrapped in a BufferedInputStream
 * @throws IOException when stream cannot be created
 */
public static InputStream getDecompressorStreamWrapper(InputStream in, int bufSize,
    String filename) throws IOException {

  if (StringUtils.endsWithIgnoreCase(filename, ".pcap")) {
    return wrap(in, bufSize);
  } else if (StringUtils.endsWithIgnoreCase(filename, ".gz")) {
    return new GzipCompressorInputStream(wrap(in, bufSize), true);
  } else if (StringUtils.endsWithIgnoreCase(filename, ".xz")) {
    return new XZInputStream(wrap(in, bufSize));
  }

  // unkown file type
  throw new ApplicationException("Could not open file with unknown extension: " + filename);
}
 
Example #7
Source File: TarXzFunction.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
protected InputStream getDecompressorStream(DecompressorDescriptor descriptor)
    throws IOException {
  return new XZInputStream(
      new BufferedInputStream(
          new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE));
}
 
Example #8
Source File: XzStepTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testXzStep() throws IOException {
  Path sourceFile =
      TestDataHelper.getTestDataScenario(this, "compression_test").resolve("step.data");
  File destinationFile = tmp.newFile("step.data.xz");

  XzStep step =
      new XzStep(
          TestProjectFilesystems.createProjectFilesystem(tmp.getRoot().toPath()),
          sourceFile,
          destinationFile.toPath(),
          /* compressionLevel -- for faster testing */ 1,
          /* keep */ true,
          XZ.CHECK_CRC32);

  ExecutionContext context = TestExecutionContext.newInstance();

  assertEquals(0, step.execute(context).getExitCode());

  ByteSource original = PathByteSource.asByteSource(sourceFile);
  ByteSource decompressed =
      new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
          return new XZInputStream(new FileInputStream(destinationFile));
        }
      };

  assertTrue(Files.exists(sourceFile));
  assertTrue(
      "Decompressed file must be identical to original.", original.contentEquals(decompressed));
}
 
Example #9
Source File: XzStepTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testXzStepDeletesOriginal() throws IOException {
  Path sourceFileOriginal =
      TestDataHelper.getTestDataScenario(this, "compression_test").resolve("step.data");
  Path sourceFile = tmp.newFile("step.data").toPath();
  Files.copy(sourceFileOriginal, sourceFile, StandardCopyOption.REPLACE_EXISTING);
  File destinationFile = tmp.newFile("step.data.xz");

  XzStep step =
      new XzStep(
          TestProjectFilesystems.createProjectFilesystem(tmp.getRoot().toPath()),
          sourceFile,
          destinationFile.toPath(),
          /* compressionLevel -- for faster testing */ 1,
          /* keep */ false,
          XZ.CHECK_CRC32);

  ExecutionContext context = TestExecutionContext.newInstance();

  assertEquals(0, step.execute(context).getExitCode());

  ByteSource original = PathByteSource.asByteSource(sourceFileOriginal);
  ByteSource decompressed =
      new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
          return new XZInputStream(new FileInputStream(destinationFile));
        }
      };

  assertFalse(Files.exists(sourceFile));
  assertTrue(
      "Decompressed file must be identical to original.", original.contentEquals(decompressed));
}
 
Example #10
Source File: BasicExtractor.java    From embedded-rabbitmq with Apache License 2.0 4 votes vote down vote up
protected InputStream getCompressedInputStream(String downloadedFile,
                                               BufferedInputStream bufferedFileInput) throws IOException {
  return new XZInputStream(bufferedFileInput);
}
 
Example #11
Source File: XZDecompressionFilter.java    From kieker with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream chainInputStream(final InputStream inputStream) throws IOException {
	return new BufferedInputStream(new XZInputStream(inputStream, this.bufferSize));
}