Browse Source

Fix extraction of long file names in Tar

PR:	15230
Submitted by:	J. David Beutel


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275040 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 22 years ago
parent
commit
6e219fff93
2 changed files with 48 additions and 0 deletions
  1. +6
    -0
      src/main/org/apache/tools/tar/TarInputStream.java
  2. +42
    -0
      src/testcases/org/apache/tools/tar/TarRoundTripTest.java

+ 6
- 0
src/main/org/apache/tools/tar/TarInputStream.java View File

@@ -275,6 +275,12 @@ public class TarInputStream extends FilterInputStream {
longName.append(new String(buffer, 0, length));
}
getNextEntry();

// remove trailing null terminator
if (longName.length() > 0
&& longName.charAt(longName.length() - 1) == 0) {
longName.deleteCharAt(longName.length() - 1);
}
this.currEntry.setName(longName.toString());
}



+ 42
- 0
src/testcases/org/apache/tools/tar/TarRoundTripTest.java View File

@@ -0,0 +1,42 @@
package org.apache.tools.tar;

import java.io.IOException;
import java.io.ByteArrayOutputStream;
import junit.framework.TestCase;

public class TarRoundTripTest extends TestCase {

private static final String LONG_NAME
= "this/path/name/contains/more/than/one/hundred/characters/in/order/"
+ "to/test/the/GNU/long/file/name/capability/round/tripped";

public TarRoundTripTest(String name) {
super(name);
}

/**
* test round-tripping long (GNU) entries
*/
public void testLongRoundTripping() throws IOException {
TarEntry original = new TarEntry(LONG_NAME);
assertEquals("over 100 chars", true, LONG_NAME.length() > 100);
assertEquals("original name", LONG_NAME, original.getName());


ByteArrayOutputStream buff = new ByteArrayOutputStream();
TarOutputStream tos = new TarOutputStream(buff);
tos.setLongFileMode(TarOutputStream.LONGFILE_GNU);
tos.putNextEntry(original);
tos.closeEntry();
tos.close();

TarInputStream tis
= new TarInputStream(new ByteArrayInputStream(buff.toByteArray()));
TarEntry tripped = tis.getNextEntry();
assertEquals("round-tripped name", LONG_NAME, tripped.getName());
assertNull("no more entries", tis.getNextEntry());
tis.close();
}
}



Loading…
Cancel
Save