From 6e219fff933d865f5f66db1476506240c691cda1 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Wed, 6 Aug 2003 15:15:27 +0000 Subject: [PATCH] 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 --- .../org/apache/tools/tar/TarInputStream.java | 6 +++ .../apache/tools/tar/TarRoundTripTest.java | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/testcases/org/apache/tools/tar/TarRoundTripTest.java diff --git a/src/main/org/apache/tools/tar/TarInputStream.java b/src/main/org/apache/tools/tar/TarInputStream.java index c599aa126..fc486a904 100644 --- a/src/main/org/apache/tools/tar/TarInputStream.java +++ b/src/main/org/apache/tools/tar/TarInputStream.java @@ -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()); } diff --git a/src/testcases/org/apache/tools/tar/TarRoundTripTest.java b/src/testcases/org/apache/tools/tar/TarRoundTripTest.java new file mode 100644 index 000000000..a3ded3196 --- /dev/null +++ b/src/testcases/org/apache/tools/tar/TarRoundTripTest.java @@ -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(); + } +} + +