You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pe...@apache.org on 2020/05/06 08:36:22 UTC

[commons-compress] branch master updated: COMPRESS-509 : add '/' to directories with long name in tar

This is an automated email from the ASF dual-hosted git repository.

peterlee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new 7cb298d  COMPRESS-509 : add '/' to directories with long name in tar
7cb298d is described below

commit 7cb298d943ec1a2811f1472824b7724d580a9217
Author: PeterAlfredLee <pe...@gmail.com>
AuthorDate: Wed May 6 16:32:58 2020 +0800

    COMPRESS-509 : add '/' to directories with long name in tar
    
    Resolve the ambiguous behavior of the TarArchiveEntry.getName() method between directory with short name and long name.
    And improve the imports of some test classes.
---
 .../archivers/tar/TarArchiveInputStream.java       |  8 ++-
 .../commons/compress/archivers/TarTestCase.java    |  6 ++-
 .../archivers/tar/TarArchiveEntryTest.java         |  2 -
 .../archivers/tar/TarArchiveInputStreamTest.java   | 59 ++++++++++++++++++++--
 4 files changed, 67 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index c02beda..716718d 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -393,7 +393,13 @@ public class TarArchiveInputStream extends ArchiveInputStream {
                 // entry
                 return null;
             }
-            currEntry.setName(zipEncoding.decode(longNameData));
+
+            // COMPRESS-509 : the name of directories should end with '/'
+            String name = zipEncoding.decode(longNameData);
+            if (currEntry.isDirectory() && !name.endsWith("/")) {
+                name += "/";
+            }
+            currEntry.setName(name);
         }
 
         if (currEntry.isGlobalPaxHeader()){ // Process Global Pax headers
diff --git a/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java b/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java
index 19fa51a..27c1e9a 100644
--- a/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java
@@ -18,7 +18,11 @@
  */
 package org.apache.commons.compress.archivers;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.FileInputStream;
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
index 703d3a4..54b372e 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
@@ -32,8 +32,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.util.Locale;
 import org.apache.commons.compress.AbstractTestCase;
 import org.junit.Test;
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
index 661e66b..b1cee11 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
@@ -18,9 +18,6 @@
 
 package org.apache.commons.compress.archivers.tar;
 
-import static org.apache.commons.compress.AbstractTestCase.getFile;
-import static org.apache.commons.compress.AbstractTestCase.mkdir;
-import static org.apache.commons.compress.AbstractTestCase.rmdir;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -28,6 +25,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -41,12 +39,15 @@ import java.util.Map;
 import java.util.TimeZone;
 import java.util.zip.GZIPInputStream;
 
+import org.apache.commons.compress.AbstractTestCase;
 import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
 import org.apache.commons.compress.utils.CharsetNames;
 import org.apache.commons.compress.utils.IOUtils;
 import org.junit.Test;
 
-public class TarArchiveInputStreamTest {
+public class TarArchiveInputStreamTest extends AbstractTestCase {
 
     @Test
     public void readSimplePaxHeader() throws Exception {
@@ -373,6 +374,56 @@ public class TarArchiveInputStreamTest {
         }
     }
 
+    @Test
+    public void testDirectoryWithLongNameEndsWithSlash() throws IOException, ArchiveException {
+        final String rootPath = dir.getAbsolutePath();
+        final String dirDirectory = "COMPRESS-509";
+        final int count = 100;
+        File root = new File(rootPath + "/" + dirDirectory);
+        root.mkdirs();
+        for (int i = 1; i < count; i++) {
+            // -----------------------
+            // create empty dirs with incremental length
+            // -----------------------
+            String subDir = "";
+            for (int j = 0; j < i; j++) {
+                subDir += "a";
+            }
+            File dir = new File(rootPath + "/" + dirDirectory, "/" + subDir);
+            dir.mkdir();
+
+            // -----------------------
+            // tar these dirs
+            // -----------------------
+            String fileName = "/" + dirDirectory + "/" + subDir;
+            File tarF = new File(rootPath + "/tar" + i + ".tar");
+            FileOutputStream dest = new FileOutputStream(tarF);
+            TarArchiveOutputStream out = new TarArchiveOutputStream(new BufferedOutputStream(dest));
+            out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
+            out.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
+
+            File file = new File(rootPath, fileName);
+            TarArchiveEntry entry = new TarArchiveEntry(file);
+            entry.setName(fileName);
+            out.putArchiveEntry(entry);
+            out.closeArchiveEntry();
+            out.flush();
+            out.close();
+
+            // -----------------------
+            // untar these tars
+            // -----------------------
+            InputStream is = new FileInputStream(tarF);
+            TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
+                    .createArchiveInputStream("tar", is);
+            TarArchiveEntry outEntry;
+            while ((outEntry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
+                assertTrue(outEntry.getName().endsWith("/"));
+            }
+            debInputStream.close();
+        }
+    }
+
     private TarArchiveInputStream getTestStream(final String name) {
         return new TarArchiveInputStream(
                 TarArchiveInputStreamTest.class.getResourceAsStream(name));