Author: tcurdt Date: Fri Jan 9 02:13:11 2009 New Revision: 732999 URL: http://svn.apache.org/viewvc?rev=732999&view=rev Log: applied a modified patch from Christian Grobmeier
add and delete support https://issues.apache.org/jira/browse/SANDBOX-183 Removed: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeWorker.java commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/DeleteChange.java commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeWorkerTest.java Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java?rev=732999&r1=732998&r2=732999&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java Fri Jan 9 02:13:11 2009 @@ -18,8 +18,65 @@ */ package org.apache.commons.compress.changes; +import java.io.InputStream; -interface Change { - // public void perform(ArchiveInputStream input); - public int type(); +import org.apache.commons.compress.archivers.ArchiveEntry; + + +public class Change { + private String targetFile = null; + private ArchiveEntry entry = null; + private InputStream input = null; + private int type = 0; + + static final int TYPE_DELETE = 1; + static final int TYPE_ADD = 2; + static final int TYPE_MOVE = 3; + + /** + * Constructor. Takes the filename of the file to be deleted + * from the stream as argument. + * @param pFilename the filename of the file to delete + */ + public Change(final String pFilename) { + if(pFilename == null) { + throw new NullPointerException(); + } + targetFile = pFilename; + type = TYPE_DELETE; + } + +// public Change(final String pOldname, final ArchiveEntry pEntry) { +// if(pOldname == null || pEntry == null) { +// throw new NullPointerException(); +// } +// targetFile = pOldname; +// entry = pEntry; +// type = TYPE_MOVE; +// } + + public Change(final ArchiveEntry pEntry, final InputStream pInput) { + if(pEntry == null || pInput == null) { + throw new NullPointerException(); + } + this.entry = pEntry; + this.input = pInput; + type = TYPE_ADD; + } + + public ArchiveEntry getEntry() { + return entry; + } + + public InputStream getInput() { + return input; + } + + public String targetFile() { + return targetFile; + } + + public int type() { + return type; + } } Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java?rev=732999&r1=732998&r2=732999&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java Fri Jan 9 02:13:11 2009 @@ -18,32 +18,72 @@ */ package org.apache.commons.compress.changes; +import java.io.IOException; import java.io.InputStream; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.ArchiveInputStream; +import org.apache.commons.compress.archivers.ArchiveOutputStream; +import org.apache.commons.compress.utils.IOUtils; public final class ChangeSet { private final Set changes = new LinkedHashSet(); - public static final int CHANGE_TYPE_DELETE = 1; - public static final int CHANGE_TYPE_ADD = 2; - - public void delete( final String pFilename ) { - changes.add(new DeleteChange(pFilename)); + changes.add(new Change(pFilename)); } - public void move( final String pFrom, final String pTo ) { - } +// public void move( final String pFrom, final String pTo ) { +// changes.add(new Change(pFrom, pTo)); +// } public void add( final ArchiveEntry pEntry, final InputStream pInput) { + changes.add(new Change(pEntry, pInput)); } public Set asSet() { return changes; } + + public void perform(ArchiveInputStream in, ArchiveOutputStream out) throws IOException { + ArchiveEntry entry = null; + while((entry = in.getNextEntry()) != null) { + boolean copy = true; + + for (Iterator it = changes.iterator(); it.hasNext();) { + Change change = (Change)it.next(); + + if(change.type() == Change.TYPE_ADD) { + copyStream(change.getInput(), out, change.getEntry()); + it.remove(); + } + + if( change.type() == Change.TYPE_DELETE && + entry.getName() != null && + entry.getName().equals(change.targetFile())) { + System.out.println("Delete: " + entry.getName()); + copy = false; + it.remove(); + break; + } + } + + if(copy) { + copyStream(in, out, entry); + } + } + } + + private static void copyStream(InputStream in, ArchiveOutputStream out, ArchiveEntry entry) throws IOException { + out.putArchiveEntry(entry); + IOUtils.copy(in, out); + out.closeArchiveEntry(); + System.out.println("Copy: " + entry.getName()); + } + } Modified: commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java?rev=732999&r1=732998&r2=732999&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java (original) +++ commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java Fri Jan 9 02:13:11 2009 @@ -18,38 +18,226 @@ */ package org.apache.commons.compress.changes; -import java.io.IOException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; import junit.framework.TestCase; -import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveInputStream; -import org.apache.commons.compress.archivers.memory.MemoryArchiveInputStream; +import org.apache.commons.compress.archivers.ArchiveOutputStream; +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.archivers.ar.ArArchiveEntry; +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; public final class ChangeSetTestCase extends TestCase { - private void apply( final ChangeSet cs ) throws IOException { - - final ArchiveInputStream is = new MemoryArchiveInputStream(new String[][] { - { "test1", "" }, - { "test2", "" }, - { "dir1/test1", "" }, - { "dir1/test2", "" }, - { "dir2/test1", "" }, - { "dir2/test2", "" } - }); - - while(true) { - final ArchiveEntry entry = is.getNextEntry(); - - if (entry == null) { - break; - } + public void testDeleteFromZip() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); - // delete, new name, new content + final File input = new File(getClass().getClassLoader().getResource("bla.zip").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("zip", is); + + File temp = File.createTempFile("test", ".zip"); + out = new ArchiveStreamFactory().createArchiveOutputStream("zip", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); + } + } + + public void testDeleteFromTar() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); + + final File input = new File(getClass().getClassLoader().getResource("bla.tar").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("tar", is); + + File temp = File.createTempFile("test", ".tar"); + out = new ArchiveStreamFactory().createArchiveOutputStream("tar", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); + } + } + + public void testDeleteFromJar() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); + changes.delete("META-INF/MANIFEST.MF"); + + final File input = new File(getClass().getClassLoader().getResource("bla.jar").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("jar", is); + + File temp = File.createTempFile("test", ".jar"); + out = new ArchiveStreamFactory().createArchiveOutputStream("jar", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); + } + } + + public void testDeleteFromAr() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); + + final File input = new File(getClass().getClassLoader().getResource("bla.ar").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("ar", is); + + File temp = File.createTempFile("test", ".ar"); + out = new ArchiveStreamFactory().createArchiveOutputStream("ar", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); } } - public void testChangeSet() { + public void testDeleteFromAndAddToZip() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); + + + final File file1 = new File(getClass().getClassLoader().getResource("test.txt").getFile()); + ZipArchiveEntry entry = new ZipArchiveEntry("testdata/test.txt"); + changes.add(entry, new FileInputStream(file1)); + + final File input = new File(getClass().getClassLoader().getResource("bla.zip").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("zip", is); + + File temp = File.createTempFile("test", ".zip"); + out = new ArchiveStreamFactory().createArchiveOutputStream("zip", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); + } + } + + public void testDeleteFromAndAddToTar() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); + + + final File file1 = new File(getClass().getClassLoader().getResource("test.txt").getFile()); + + final TarArchiveEntry entry = new TarArchiveEntry("testdata/test.txt"); + entry.setModTime(0); + entry.setSize(file1.length()); + entry.setUserId(0); + entry.setGroupId(0); + entry.setUserName("avalon"); + entry.setGroupName("excalibur"); + entry.setMode(0100000); + + changes.add(entry, new FileInputStream(file1)); + + final File input = new File(getClass().getClassLoader().getResource("bla.tar").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("tar", is); + + File temp = File.createTempFile("test", ".tar"); + out = new ArchiveStreamFactory().createArchiveOutputStream("tar", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); + } + } + + public void testDeleteFromAndAddToJar() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); + + final File file1 = new File(getClass().getClassLoader().getResource("test.txt").getFile()); + JarArchiveEntry entry = new JarArchiveEntry("testdata/test.txt"); + changes.add(entry, new FileInputStream(file1)); + + final File input = new File(getClass().getClassLoader().getResource("bla.jar").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("jar", is); + + File temp = File.createTempFile("test", ".jar"); + out = new ArchiveStreamFactory().createArchiveOutputStream("jar", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); + } + } + + public void testDeleteFromAndAddToAr() throws Exception { + ArchiveOutputStream out = null; + ArchiveInputStream ais = null; + try { + ChangeSet changes = new ChangeSet(); + changes.delete("test2.xml"); + + final File file1 = new File(getClass().getClassLoader().getResource("test.txt").getFile()); + + final ArArchiveEntry entry = new ArArchiveEntry("test.txt", file1.length()); + + changes.add(entry, new FileInputStream(file1)); + + final File input = new File(getClass().getClassLoader().getResource("bla.ar").getFile()); + final InputStream is = new FileInputStream(input); + ais = new ArchiveStreamFactory().createArchiveInputStream("ar", is); + + File temp = File.createTempFile("test", ".ar"); + out = new ArchiveStreamFactory().createArchiveOutputStream("ar", new FileOutputStream(temp)); + + System.out.println(temp.getAbsolutePath()); + changes.perform(ais, out); + } finally { + if(out != null) out.close(); + if(ais != null) ais.close(); + } } }
