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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6aa2260  [VFS-664] fix can't get children file with special name in 
zip file (#95)
6aa2260 is described below

commit 6aa2260d900d47c69d07de7f7b8a67786b0d9dc8
Author: Lee <55973914+peteralfred...@users.noreply.github.com>
AuthorDate: Mon Sep 7 06:58:47 2020 +0800

    [VFS-664] fix can't get children file with special name in zip file (#95)
    
    1.fix and add a test for read children file with special name in zip file
    2.fix comment error in 
commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.encoding(String)
    3.fix similiar problems in tar, tgz and tbz2
---
 .../commons/vfs2/provider/AbstractFileObject.java  |   3 +-
 .../apache/commons/vfs2/provider/UriParser.java    |   2 +-
 .../commons/vfs2/provider/tar/TarFileSystem.java   |   3 +-
 .../commons/vfs2/provider/zip/ZipFileSystem.java   |   3 +-
 .../provider/tar/test/TarFileObjectTestCase.java   |  77 +++++++++++++++++++++
 .../provider/zip/test/ZipFileObjectTestCase.java   |  24 +++++++
 .../test/resources/test-data/special_fileName.tar  | Bin 0 -> 20480 bytes
 .../test/resources/test-data/special_fileName.tbz2 | Bin 0 -> 271 bytes
 .../test/resources/test-data/special_fileName.tgz  | Bin 0 -> 285 bytes
 .../test/resources/test-data/special_fileName.zip  | Bin 0 -> 1846 bytes
 10 files changed, 108 insertions(+), 4 deletions(-)

diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
index 1ac2677..10e0248 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
@@ -1041,8 +1041,9 @@ public abstract class AbstractFileObject<AFS extends 
AbstractFileSystem> impleme
         final FileObject[] children = getChildren();
         for (final FileObject element : children) {
             final FileName child = element.getName();
+            final String childBaseName = child.getBaseName();
             // TODO - use a comparator to compare names
-            if (child.getBaseName().equals(name)) {
+            if (childBaseName.equals(name) || 
UriParser.decode(childBaseName).equals(name)) {
                 return resolveFile(child);
             }
         }
diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java
index 4ef97c5..9faf1af 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java
@@ -171,7 +171,7 @@ public final class UriParser {
     }
 
     /**
-     * Removes %nn encodings from a string.
+     * Converts "special" characters to their %nn value.
      *
      * @param decodedStr The decoded String.
      * @return The encoded String.
diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileSystem.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileSystem.java
index 739cc79..51a8b35 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileSystem.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileSystem.java
@@ -44,6 +44,7 @@ import org.apache.commons.vfs2.provider.bzip2.Bzip2FileObject;
  * A read-only file system for Tar files.
  */
 public class TarFileSystem extends AbstractFileSystem {
+    private final char[] ENC = {'!'};
 
     private static final int DEFAULT_INDEX_SIZE = 100;
 
@@ -79,7 +80,7 @@ public class TarFileSystem extends AbstractFileSystem {
             TarArchiveEntry entry;
             while ((entry = getTarFile().getNextTarEntry()) != null) {
                 final AbstractFileName name = (AbstractFileName) 
getFileSystemManager().resolveName(getRootName(),
-                        UriParser.encode(entry.getName()));
+                        UriParser.encode(entry.getName(), ENC));
 
                 // Create the file
                 TarFileObject fileObj;
diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java
index 2f3ea74..e49bca4 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java
@@ -45,6 +45,7 @@ import org.apache.commons.vfs2.provider.UriParser;
  * A read-only file system for ZIP and JAR files.
  */
 public class ZipFileSystem extends AbstractFileSystem {
+    private final char[] ENC = {'!'};
 
     private static final Log LOG = LogFactory.getLog(ZipFileSystem.class);
 
@@ -84,7 +85,7 @@ public class ZipFileSystem extends AbstractFileSystem {
             while (entries.hasMoreElements()) {
                 final ZipEntry entry = entries.nextElement();
                 final AbstractFileName name = (AbstractFileName) 
getFileSystemManager().resolveName(getRootName(),
-                        UriParser.encode(entry.getName()));
+                        UriParser.encode(entry.getName(), ENC));
 
                 // Create the file
                 ZipFileObject fileObj;
diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/tar/test/TarFileObjectTestCase.java
 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/tar/test/TarFileObjectTestCase.java
new file mode 100644
index 0000000..9072160
--- /dev/null
+++ 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/tar/test/TarFileObjectTestCase.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs2.provider.tar.test;
+
+import org.apache.commons.vfs2.FileObject;
+import org.apache.commons.vfs2.FileSystemException;
+import org.apache.commons.vfs2.FileSystemManager;
+import org.apache.commons.vfs2.VFS;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+
+
+public class TarFileObjectTestCase {
+
+    /**
+     * Test read file with special name in a tar file
+     */
+    @Test
+    public void testReadSpecialNameFileInTarFile() throws FileSystemException {
+
+        
testReadSpecialNameFileInFile("src/test/resources/test-data/special_fileName.tar",
 "tar");
+    }
+
+    /**
+     * Test read file with special name in a tgz file
+     */
+    @Test
+    public void testReadSpecialNameFileInTgzFile() throws FileSystemException {
+
+        
testReadSpecialNameFileInFile("src/test/resources/test-data/special_fileName.tgz",
 "tgz");
+    }
+
+    /**
+     * Test read file with special name in a tbz2 file
+     */
+    @Test
+    public void testReadSpecialNameFileInTbz2File() throws FileSystemException 
{
+
+        
testReadSpecialNameFileInFile("src/test/resources/test-data/special_fileName.tbz2",
 "tbz2");
+    }
+
+    private void testReadSpecialNameFileInFile(String testFilePath, String 
scheme) throws FileSystemException {
+
+        final File testFile = new File(testFilePath);
+        final String[] fileNames = {"file.txt", "file^.txt", "file~.txt", 
"file?.txt", "file@.txt", "file$.txt",
+                                    "file*.txt", "file&.txt", "file#.txt", 
"file%.txt", "file!.txt"};
+        final FileSystemManager manager = VFS.getManager();
+        final String baseUrl = scheme + ":file:" + testFile.getAbsolutePath();
+
+        // test
+        try (final FileObject fileObject = manager.resolveFile(baseUrl)) {
+            // test getChildren() number equal
+            Assert.assertEquals(fileObject.getChildren().length, 
fileNames.length);
+
+            // test getChild(String)
+            for (final String fileName : fileNames) {
+                Assert.assertNotNull("can't read file " + fileName, 
fileObject.getChild(fileName));
+            }
+        }
+    }
+}
diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/test/ZipFileObjectTestCase.java
 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/test/ZipFileObjectTestCase.java
index af16fee..32dc972 100644
--- 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/test/ZipFileObjectTestCase.java
+++ 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/test/ZipFileObjectTestCase.java
@@ -179,4 +179,28 @@ public class ZipFileObjectTestCase {
         }
         assertDelete(newZipFile);
     }
+
+    /**
+     * Test read file with special name in a zip file
+     */
+    @Test
+    public void testReadSpecialNameFileInZipFile() throws FileSystemException {
+
+        final File testFile = new 
File("src/test/resources/test-data/special_fileName.zip");
+        final String[] fileNames = {"file.txt", "file^.txt", "file~.txt", 
"file?.txt", "file@.txt", "file$.txt",
+                                    "file*.txt", "file&.txt", "file#.txt", 
"file%.txt", "file!.txt"};
+        final FileSystemManager manager = VFS.getManager();
+        final String baseUrl = "zip:file:"+testFile.getAbsolutePath();
+
+        // test
+        try (final FileObject fileObject = manager.resolveFile(baseUrl)) {
+            // test getChildren() number equal
+            Assert.assertEquals(fileObject.getChildren().length, 
fileNames.length);
+
+            // test getChild(String)
+            for (final String fileName : fileNames) {
+                Assert.assertNotNull("can't read file " + fileName, 
fileObject.getChild(fileName));
+            }
+        }
+    }
 }
diff --git a/commons-vfs2/src/test/resources/test-data/special_fileName.tar 
b/commons-vfs2/src/test/resources/test-data/special_fileName.tar
new file mode 100644
index 0000000..101ddb8
Binary files /dev/null and 
b/commons-vfs2/src/test/resources/test-data/special_fileName.tar differ
diff --git a/commons-vfs2/src/test/resources/test-data/special_fileName.tbz2 
b/commons-vfs2/src/test/resources/test-data/special_fileName.tbz2
new file mode 100644
index 0000000..cf0b7a1
Binary files /dev/null and 
b/commons-vfs2/src/test/resources/test-data/special_fileName.tbz2 differ
diff --git a/commons-vfs2/src/test/resources/test-data/special_fileName.tgz 
b/commons-vfs2/src/test/resources/test-data/special_fileName.tgz
new file mode 100644
index 0000000..428198c
Binary files /dev/null and 
b/commons-vfs2/src/test/resources/test-data/special_fileName.tgz differ
diff --git a/commons-vfs2/src/test/resources/test-data/special_fileName.zip 
b/commons-vfs2/src/test/resources/test-data/special_fileName.zip
new file mode 100644
index 0000000..ab37380
Binary files /dev/null and 
b/commons-vfs2/src/test/resources/test-data/special_fileName.zip differ

Reply via email to