diff --git a/src/main/org/apache/tools/tar/TarInputStream.java b/src/main/org/apache/tools/tar/TarInputStream.java index 73e90e955..934befe0e 100644 --- a/src/main/org/apache/tools/tar/TarInputStream.java +++ b/src/main/org/apache/tools/tar/TarInputStream.java @@ -302,11 +302,11 @@ public class TarInputStream extends FilterInputStream { if (currEntry.isGNULongNameEntry()) { // read in the name - StringBuffer longName = new StringBuffer(); + ByteArrayOutputStream longName = new ByteArrayOutputStream(); byte[] buf = new byte[SMALL_BUFFER_SIZE]; int length = 0; while ((length = read(buf)) >= 0) { - longName.append(new String(buf, 0, length)); // TODO default charset? + longName.write(buf, 0, length); } getNextEntry(); if (currEntry == null) { @@ -314,12 +314,19 @@ public class TarInputStream extends FilterInputStream { // Malformed tar file - long entry name not followed by entry return null; } - // remove trailing null terminator - if (longName.length() > 0 - && longName.charAt(longName.length() - 1) == 0) { - longName.deleteCharAt(longName.length() - 1); + byte[] longNameData = longName.toByteArray(); + // remove trailing null terminator(s) + length = longNameData.length; + while (length > 0 && longNameData[length - 1] == 0) { + --length; } - currEntry.setName(longName.toString()); + if (length != longNameData.length) { + byte[] l = new byte[length]; + System.arraycopy(longNameData, 0, l, 0, length); + longNameData = l; + } + + currEntry.setName(encoding.decode(longNameData)); } if (currEntry.isPaxHeader()){ // Process Pax headers