0

I am trying out vcdiff for creating a diff file from source and target files. Also, will be applying the diff on source file to get the target file.

I have achieved the above use case with xdelta linux command line tool.

But how to achieve the same using vcdiff-java APIs ? Any hints or directions will be useful to get started.

Thanks.

seeker
  • 2,843
  • 4
  • 25
  • 41

1 Answers1

0

I think basic use case of diff and apply can be handled as follows. Here, diff is generated from source.txt and target.txt. Then, diff is applied on source.txt to get result.txt which is equal to target.txt.

Path source = Files.createFile(Paths.get(basePath + "source.txt"));
        Files.write(source, new StringBuilder(
                "First line of the file.\n"
                + "Second line of the file."
                        ).toString().getBytes());

        Path target = Files.createFile(Paths.get(basePath + "target.txt"));
        Files.write(target, "1. First line of the file!".getBytes());

        final ByteArrayOutputStream delta_ = new ByteArrayOutputStream();

        VCDiffEncoder<OutputStream> encoder = VCDiffEncoderBuilder.builder()
                .withDictionary(Files.readAllBytes(source))
                .withTargetMatches(false)
                .withChecksum(true)
                .withInterleaving(true)
                .buildSimple();

        encoder.encode(Files.readAllBytes(target), delta_);

        ByteArrayOutputStream result_out = new ByteArrayOutputStream();
        VCDiffDecoder decoder = VCDiffDecoderBuilder.builder().buildSimple();
        decoder.decode(Files.readAllBytes(source), delta_.toByteArray(), result_out);

        Path result = Files.createFile(Paths.get(basePath + "result.txt"));
        Files.write(result, result_out.toByteArray());
seeker
  • 2,843
  • 4
  • 25
  • 41