Author: bodewig
Date: Thu Dec  8 16:12:52 2011
New Revision: 1211943

URL: http://svn.apache.org/viewvc?rev=1211943&view=rev
Log:
Support the POSIX way of writing tar entries with names longer than 100 chars.  
COMPRESS-166

Modified:
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
    
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java?rev=1211943&r1=1211942&r2=1211943&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
 Thu Dec  8 16:12:52 2011
@@ -45,6 +45,9 @@ public class TarArchiveOutputStream exte
     /** GNU tar extensions are used to store long file names in the archive. */
     public static final int LONGFILE_GNU = 2;
 
+    /** POSIX/PAX extensions are used to store long file names in the archive. 
*/
+    public static final int LONGFILE_POSIX = 3;
+
     /** Fail if a big file (> 8GiB) is required in the archive. */
     public static final int BIGFILE_ERROR = 0;
 
@@ -213,7 +216,9 @@ public class TarArchiveOutputStream exte
         Map<String, String> paxHeaders = new HashMap<String, String>();
         if (entry.getName().length() >= TarConstants.NAMELEN) {
 
-            if (longFileMode == LONGFILE_GNU) {
+            if (longFileMode == LONGFILE_POSIX) {
+                paxHeaders.put("path", entry.getName());
+            } else if (longFileMode == LONGFILE_GNU) {
                 // create a TarEntry for the LongLink, the contents
                 // of which are the entry's name
                 TarArchiveEntry longLinkEntry = new 
TarArchiveEntry(TarConstants.GNU_LONGLINK,
@@ -385,8 +390,8 @@ public class TarArchiveOutputStream exte
     void writePaxHeaders(String entryName,
                          Map<String, String> headers) throws IOException {
         String name = "./PaxHeaders.X/" + entryName;
-        if (name.length() > TarConstants.NAMELEN) {
-            name = name.substring(0, TarConstants.NAMELEN);
+        if (name.length() >= TarConstants.NAMELEN) {
+            name = name.substring(0, TarConstants.NAMELEN - 1);
         }
         TarArchiveEntry pex = new TarArchiveEntry(name,
                                                   
TarConstants.LF_PAX_EXTENDED_HEADER_LC);

Modified: 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java?rev=1211943&r1=1211942&r2=1211943&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
 (original)
+++ 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
 Thu Dec  8 16:12:52 2011
@@ -182,4 +182,26 @@ public class TarArchiveOutputStreamTest 
 
         return bos.toByteArray();
     }
+
+    public void testWriteLongFileNamePosixMode() throws Exception {
+        String n = "01234567890123456789012345678901234567890123456789"
+            + "01234567890123456789012345678901234567890123456789"
+            + "01234567890123456789012345678901234567890123456789";
+        TarArchiveEntry t =
+            new TarArchiveEntry(n);
+        t.setSize(10 * 1024);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
+        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
+        tos.putArchiveEntry(t);
+        tos.write(new byte[10 * 1024]);
+        tos.closeArchiveEntry();
+        byte[] data = bos.toByteArray();
+        assertEquals("160 path=" + n + "\n",
+                     new String(data, 512, 160, "UTF-8"));
+        TarArchiveInputStream tin =
+            new TarArchiveInputStream(new ByteArrayInputStream(data));
+        TarArchiveEntry e = tin.getNextTarEntry();
+        assertEquals(n, e.getName());
+    }
 }
\ No newline at end of file


Reply via email to