com.google.archivepatcher.applier.FileByFileV1DeltaApplier Java Examples

The following examples show how to use com.google.archivepatcher.applier.FileByFileV1DeltaApplier. 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: SamplePatchApplier.java    From archive-patcher with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) throws Exception {
  if (!new DefaultDeflateCompatibilityWindow().isCompatible()) {
    System.err.println("zlib not compatible on this system");
    System.exit(-1);
  }
  File oldFile = new File(args[0]); // must be a zip archive
  Inflater uncompressor = new Inflater(true);
  try (FileInputStream compressedPatchIn = new FileInputStream(args[1]);
      InflaterInputStream patchIn =
          new InflaterInputStream(compressedPatchIn, uncompressor, 32768);
      FileOutputStream newFileOut = new FileOutputStream(args[2])) {
    new FileByFileV1DeltaApplier().applyDelta(oldFile, patchIn, newFileOut);
  } finally {
    uncompressor.end();
  }
}
 
Example #2
Source File: FileByFileTool.java    From archive-patcher with Apache License 2.0 5 votes vote down vote up
/**
 * Apply a specified patch to the specified old file, creating the specified new file.
 * @param oldFile the old file (will be read)
 * @param patchFile the patch file (will be read)
 * @param newFile the new file (will be written)
 * @throws IOException if anything goes wrong
 */
public static void applyPatch(File oldFile, File patchFile, File newFile) throws IOException {
  // Figure out temp directory
  File tempFile = File.createTempFile("fbftool", "tmp");
  File tempDir = tempFile.getParentFile();
  tempFile.delete();
  FileByFileV1DeltaApplier applier = new FileByFileV1DeltaApplier(tempDir);
  try (FileInputStream patchIn = new FileInputStream(patchFile);
      BufferedInputStream bufferedPatchIn = new BufferedInputStream(patchIn);
      FileOutputStream newOut = new FileOutputStream(newFile);
      BufferedOutputStream bufferedNewOut = new BufferedOutputStream(newOut)) {
    applier.applyDelta(oldFile, bufferedPatchIn, bufferedNewOut);
    bufferedNewOut.flush();
  }
}
 
Example #3
Source File: ClientLoader.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void applyPatch() throws IOException
{
	byte[] vanillaHash = new byte[64];
	byte[] appliedPatchHash = new byte[64];

	try (InputStream is = ClientLoader.class.getResourceAsStream("/client.serial"))
	{
		if (is == null)
		{
			SwingUtilities.invokeLater(() ->
				new FatalErrorDialog("The client-patch is missing from the classpath. If you are building " +
					"the client you need to re-run maven")
					.addBuildingGuide()
					.open());
			throw new NullPointerException();
		}

		DataInputStream dis = new DataInputStream(is);
		dis.readFully(vanillaHash);
		dis.readFully(appliedPatchHash);
	}

	byte[] vanillaCacheHash = Files.asByteSource(VANILLA_CACHE).hash(Hashing.sha512()).asBytes();
	if (!Arrays.equals(vanillaHash, vanillaCacheHash))
	{
		log.info("Client is outdated!");
		updateCheckMode = VANILLA;
		return;
	}

	if (PATCHED_CACHE.exists())
	{
		byte[] diskBytes = Files.asByteSource(PATCHED_CACHE).hash(Hashing.sha512()).asBytes();
		if (!Arrays.equals(diskBytes, appliedPatchHash))
		{
			log.warn("Cached patch hash mismatches, regenerating patch");
		}
		else
		{
			log.info("Using cached patched client");
			return;
		}
	}

	try (HashingOutputStream hos = new HashingOutputStream(Hashing.sha512(), new FileOutputStream(PATCHED_CACHE));
		InputStream patch = ClientLoader.class.getResourceAsStream("/client.patch"))
	{
		new FileByFileV1DeltaApplier().applyDelta(VANILLA_CACHE, patch, hos);

		if (!Arrays.equals(hos.hash().asBytes(), appliedPatchHash))
		{
			log.error("Patched client hash mismatch");
			updateCheckMode = VANILLA;
			return;
		}
	}
	catch (IOException e)
	{
		log.error("Unable to apply patch despite hash matching", e);
		updateCheckMode = VANILLA;
		return;
	}
}
 
Example #4
Source File: FileByFileV1IntegrationTest.java    From archive-patcher with Apache License 2.0 4 votes vote down vote up
/**
 * High-level integration test that covers the most common kinds of operations expected to be
 * found in the real world.
 */
@Test
public void testPatchAndApply() throws Exception {
  // Write the old archive to disk.
  byte[] oldArchiveBytes = UnitTestZipArchive.makeTestZip(Arrays.asList(
      OLD_ENTRY1,
      OLD_ENTRY2,
      OLD_ENTRY3,
      OLD_ENTRY4,
      OLD_ENTRY5,
      OLD_ENTRY6,
      OLD_ENTRY7,
      OLD_ENTRY8,
      OLD_ENTRY9,
      OLD_ENTRY10,
      OLD_ENTRY11,
      OLD_ENTRY12,
      OLD_ENTRY13));
  writeFile(oldFile, oldArchiveBytes);

  // Write the new archive to disk. Flip the order around to fully exercise reordering logic where
  // the offsets might otherwise be exactly the same by chance.
  List<UnitTestZipEntry> newEntries = Arrays.asList(
      NEW_ENTRY1,
      NEW_ENTRY2,
      NEW_ENTRY3,
      NEW_ENTRY4,
      NEW_ENTRY5,
      NEW_ENTRY6,
      NEW_ENTRY7,
      NEW_ENTRY8,
      NEW_ENTRY9,
      NEW_ENTRY10,
      NEW_ENTRY11,
      NEW_ENTRY12,
      NEW_ENTRY13);
  Collections.reverse(newEntries);
  byte[] newArchiveBytes = UnitTestZipArchive.makeTestZip(newEntries);
  writeFile(newFile, newArchiveBytes);

  // Generate the patch.
  ByteArrayOutputStream patchBuffer = new ByteArrayOutputStream();
  FileByFileV1DeltaGenerator generator = new FileByFileV1DeltaGenerator();
  generator.generateDelta(oldFile, newFile, patchBuffer);

  // Apply the patch.
  FileByFileV1DeltaApplier applier = new FileByFileV1DeltaApplier(tempDir);
  ByteArrayInputStream patchIn = new ByteArrayInputStream(patchBuffer.toByteArray());
  ByteArrayOutputStream newOut = new ByteArrayOutputStream();
  applier.applyDelta(oldFile, patchIn, newOut);

  // Finally, expect that the result of applying the patch is exactly the same as the new archive
  // that was written to disk.
  Assert.assertArrayEquals(newArchiveBytes, newOut.toByteArray());
}