importcom.google.archivepatcher.generator.FileByFileV1DeltaGenerator;importcom.google.archivepatcher.shared.DefaultDeflateCompatibilityWindow;importjava.io.File;importjava.io.FileOutputStream;importjava.util.zip.Deflater;importjava.util.zip.DeflaterOutputStream;/** Generate a patch; args are old file path, new file path, and patch file path. */publicclassSamplePatchGenerator{publicstaticvoidmain(String... args)throwsException{if(!newDefaultDeflateCompatibilityWindow().isCompatible()){System.err.println("zlib not compatible on this system");System.exit(-1);}File oldFile =newFile(args[0]);// must be a zip archiveFile newFile =newFile(args[1]);// must be a zip archiveDeflater compressor =newDeflater(9,true);// to compress the patchtry(FileOutputStream patchOut =newFileOutputStream(args[2]);// args[2]为补丁存放地址DeflaterOutputStream compressedPatchOut =newDeflaterOutputStream(patchOut, compressor,32768)){newFileByFileV1DeltaGenerator().generateDelta(oldFile, newFile, compressedPatchOut);
compressedPatchOut.finish();
compressedPatchOut.flush();}finally{
compressor.end();}}}
应用补丁
importcom.google.archivepatcher.applier.FileByFileV1DeltaApplier;importcom.google.archivepatcher.shared.DefaultDeflateCompatibilityWindow;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.util.zip.Inflater;importjava.util.zip.InflaterInputStream;/** Apply a patch; args are old file path, patch file path, and new file path. */publicclassSamplePatchApplier{publicstaticvoidmain(String... args)throwsException{if(!newDefaultDeflateCompatibilityWindow().isCompatible()){System.err.println("zlib not compatible on this system");System.exit(-1);}File oldFile =newFile(args[0]);// must be a zip archive args[0]为旧版本文件地址Inflater uncompressor =newInflater(true);// to uncompress the patchtry(FileInputStream compressedPatchIn =newFileInputStream(args[1]);// args[1]补丁文件地址InflaterInputStream patchIn =newInflaterInputStream(compressedPatchIn, uncompressor,32768);FileOutputStream newFileOut =newFileOutputStream(args[2])){// args[2]合成文件地址newFileByFileV1DeltaApplier().applyDelta(oldFile, patchIn, newFileOut);}finally{
uncompressor.end();}}}