Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding DeltaZipCLI - a command-line tool for DeltaZip file manipulation.

  • Loading branch information...
commit aacf60fc9e23f41cd233d020835626711f8ffd9a 1 parent 69a733a
@eriksoe eriksoe authored
View
5 src/main/java/com/trifork/deltazip/DZUtil.java
@@ -45,6 +45,11 @@ public FileAccess(File f) throws IOException {
this.file = new RandomAccessFile(f, "r").getChannel();
}
+ public FileAccess(File f, boolean write) throws IOException {
+ String mode = write? "rw":"r";
+ this.file = new RandomAccessFile(f, mode).getChannel();
+ }
+
public void close() throws IOException {
file.close();
}
View
20 src/main/java/com/trifork/deltazip/DeltaZip.java
@@ -1,5 +1,6 @@
package com.trifork.deltazip;
+import java.util.Iterator;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;
@@ -88,6 +89,25 @@ public AppendSpecification add(ByteBuffer new_version) throws IOException {
return new AppendSpecification(current_pos, baos.toByteArray());
}
+ /** Computes an AppendSpecification for adding a version.
+ * Has the side effect of placing the cursor at the end.
+ */
+ public AppendSpecification add(Iterator<ByteBuffer> versions_to_add) throws IOException {
+ set_cursor_at_end();
+ ExtByteArrayOutputStream baos = new ExtByteArrayOutputStream();
+ ByteBuffer prev_version = get();
+
+ while (versions_to_add.hasNext()) {
+ ByteBuffer cur = versions_to_add.next();
+ if (prev_version != null) pack_compressed(prev_version, allToByteArray(cur), baos);
+ prev_version = cur;
+ }
+
+ pack_uncompressed(prev_version, baos);
+
+ return new AppendSpecification(current_pos, baos.toByteArray());
+ }
+
//==================== Internals =======================================
protected void set_cursor_at_end() throws IOException {
View
143 src/main/java/com/trifork/deltazip/DeltaZipCLI.java
@@ -0,0 +1,143 @@
+package com.trifork.deltazip;
+
+import com.trifork.deltazip.DZUtil.FileAccess;
+import com.trifork.deltazip.DeltaZip.AppendSpecification;
+import java.io.File;
+import java.io.IOException;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
+public abstract class DeltaZipCLI {
+
+ public static void main(String[] args) throws IOException {
+ if (args.length < 1) {usage(); System.exit(1);}
+
+ String command = args[0];
+ if ("count".equals(command)) do_count(args);
+ else if ("get".equals(command)) do_get(args);
+ else if ("create".equals(command)) do_create(args);
+ else if ("add".equals(command)) do_add(args);
+ else {usage(); System.exit(1);}
+ }
+
+ public static void usage() {
+ System.err.println("Usage: deltazip [COMMAND] [ARGS]");
+ System.err.println("Commands:");
+ System.err.println(" create [dzfile] [version-files]");
+ System.err.println(" get [dzfile] Print the last version");
+ System.err.println(" get @n [dzfile] Print the nth-last version");
+ System.err.println(" count [dzfile] Count the number of versions");
+ System.err.println(" add [dzfile] [version-files]");
+ }
+
+ public static void do_count(String[] args) throws IOException {
+ if (args.length != 2) {usage(); System.exit(1);}
+ FileAccess fa = openDZFile(args[1]);
+ DeltaZip dz = new DeltaZip(fa);
+
+ int count = 0;
+ if (dz.get() != null) {
+ count++;
+ while (dz.hasPrevious()) {
+ dz.previous();
+ count++;
+ }
+ }
+ fa.close();
+ System.out.println(count);
+ }
+
+ public static void do_get(String[] args) throws IOException {
+ if (args.length < 2) {usage(); System.exit(1);}
+ int rev_nr = 0;
+ int file_arg = 1;
+ if (args[file_arg].startsWith("@")) {
+ rev_nr = Integer.parseInt(args[file_arg].substring(1));
+ file_arg++;
+ }
+ if (args.length != file_arg+1) {usage(); System.exit(1);}
+
+ FileAccess fa = openDZFile(args[file_arg]);
+ DeltaZip dz = new DeltaZip(fa);
+
+ if (dz.get() == null) {
+ System.err.println("Archive is empty.");
+ System.exit(3);
+ } else {
+ for (int i=0; i<rev_nr; i++) {
+ if (! dz.hasPrevious()) {
+ System.err.println("Archive only contains "+(i+1)+" versions.");
+ System.exit(3);
+ }
+ dz.previous();
+ }
+ System.out.write(DeltaZip.allToByteArray(dz.get()));
+ }
+ fa.close();
+ }
+
+ public static void do_create(final String[] args) throws IOException {
+ if (args.length < 2) {usage(); System.exit(1);}
+ String filename = args[1];
+ File dzfile = new File(filename);
+ if (dzfile.exists()) {
+ System.err.println("File already exists: "+filename);
+ System.exit(2);
+ }
+
+ // Possible race condition here. Can't do anything about it I think.
+ FileAccess fa = new FileAccess(dzfile, true);
+ DeltaZip dz = new DeltaZip(fa);
+
+ Iterator<ByteBuffer> it = new Iterator<ByteBuffer>() {
+ int arg_pos = 2;
+ public boolean hasNext() {return arg_pos < args.length;}
+ public ByteBuffer next() {
+ String filename = args[arg_pos++];
+ try {
+ return fileToByteBuffer(filename);
+ } catch (IOException ioe) {
+ throw new RuntimeException(ioe);
+ }
+ }
+ public void remove() {throw new UnsupportedOperationException();}
+ };
+
+ AppendSpecification app_spec = dz.add(it);
+ fa.applyAppendSpec(app_spec);
+ }
+
+ public static void do_add(String[] args) throws IOException { /*TODO*/ }
+
+ //======================================================================
+ private static FileAccess openDZFile(String filename) throws IOException {
+ File dzfile = new File(filename);
+ checkExistence(dzfile);
+ return new FileAccess(dzfile);
+ }
+
+ private static void checkExistence(File file) {
+ if (! file.exists()) {
+ System.err.println("No such file: "+file);
+ System.exit(2);
+ }
+ }
+
+ private static ByteBuffer fileToByteBuffer(String filename) throws IOException {
+ FileInputStream in = new FileInputStream(filename);
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] tmp = new byte[4096];
+ int r;
+ while ((r = in.read(tmp)) >= 0) {
+ baos.write(tmp, 0, r);
+ }
+ return ByteBuffer.wrap(baos.toByteArray());
+ } finally {
+ in.close();
+ }
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.