访问文件时TPath忽略大小写[Java TrueZip]

时间:2017-03-18 19:56:59

标签: java case truezip

有没有办法在存档中访问文件而忽略使用TrueZip的文件名大小写?

想象一下zip存档的内容:

MyZip.zip
-> myFolder/tExtFile.txt
-> anotherFolder/TextFiles/file.txt
-> myFile.txt
-> anotherFile.txt
-> OneMOREfile.txt

这是它的工作原理:

TPath tPath = new TPath("MyZip.zip\\myFolder\\tExtFile.txt");
System.out.println(tPath.toFile().getName()); //prints tExtFile.txt 

如何做同样但忽略所有情况,如下:

// note "myFolder" changed to "myfolder" and "tExtFile" to "textfile"    
TPath tPath = new TPath("MyZip.zip\\myfolder\\textfile.txt");
System.out.println(tPath.toFile().getName()); // should print tExtFile.txt

上面的代码会引发FsEntryNotFoundException ... (no such entry)

适用于常规java.io.File,不确定为什么不是TrueZip的TFile或我遗失了什么?

我的目标是仅使用小写文件和文件夹访问每个文件。

编辑:24-03-2017

假设我想从提到的zip存档MyZip.zip

中的文件中读取字节
Path tPath = new TPath("...MyZip.zip\\myFolder\\tExtFile.txt");
byte[] bytes = Files.readAllBytes(tPath); //returns bytes of the file 

上面的这个代码段有效,但下面的这个代码不起作用(抛出提到 - > FsEntryNotFoundException)。它是相同的路径和文件只是小写。

Path tPath = new TPath("...myzip.zip\\myfolder\\textfile.txt");
byte[] bytes = Files.readAllBytes(tPath);

2 个答案:

答案 0 :(得分:1)

你说:

  

我的目标是只使用文件和文件夹的小写来访问每个文件。

但是一厢情愿的想法不会让你在这里走得很远。事实上,大多数文件系统(Windows类型除外)都区分大小写,即如果使用大写或小写字符,它们会产生很大的不同。在那里,您甚至可以在同一目录中多次使用不同情况下的“相同”文件名。即如果名称为file.txtFile.txtfile.TXT,它实际上会有所不同。 Windows实际上是一个例外,但TrueZIP不会模拟Windows文件系统,而是模拟适用于所有平台上的ZIP,TAR等的通用存档文件系统。因此,您无法选择使用大写或小写字符,但必须完全按照存档在ZIP存档中使用它们。

更新:就像一个小小的证据,我登录了一个带有extfs文件系统的远程Linux盒子,并做了这个:

~$ mkdir test
~$ cd test
~/test$ touch file.txt
~/test$ touch File.txt
~/test$ touch File.TXT
~/test$ ls -l
total 0
-rw-r--r-- 1 group user 0 Mar 25 00:14 File.TXT
-rw-r--r-- 1 group user 0 Mar 25 00:14 File.txt
-rw-r--r-- 1 group user 0 Mar 25 00:14 file.txt

正如您可以清楚地看到的,有三个不同的文件,而不仅仅是一个。

如果将这三个文件压缩到存档中会发生什么?

~/test$ zip ../files.zip *
  adding: File.TXT (stored 0%)
  adding: File.txt (stored 0%)
  adding: file.txt (stored 0%)

添加了三个文件。但他们是否仍然会在档案中记录文件或只是以一个名字存储?

~/test$ unzip -l ../files.zip
Archive:  ../files.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2017-03-25 00:14   File.TXT
        0  2017-03-25 00:14   File.txt
        0  2017-03-25 00:14   file.txt
---------                     -------
        0                     3 files

“3档”,它说 - quod erat demonstrandum。

正如您所看到的,Windows不是整个世界。但是,如果您将该存档复制到Windows框并在那里解压缩,它只会将一个文件写入具有NTFS或FAT文件系统的磁盘 - 这是运气问题。如果这三个文件的内容不同,则非常糟糕。

更新2:好的,由于上面详细说明的原因,TrueZIP中没有解决方案,但如果你想解决它,你可以像这样手动完成:

package de.scrum_master.app;

import de.schlichtherle.truezip.nio.file.TPath;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;

public class Application {
  public static void main(String[] args) throws IOException, URISyntaxException {
    TPathHelper tPathHelper = new TPathHelper(
      new TPath(
        "../../../downloads/powershellarsenal-master.zip/" +
          "PowerShellArsenal-master\\LIB/CAPSTONE\\LIB\\X64\\LIBCAPSTONE.DLL"
      )
    );
    TPath caseSensitivePath = tPathHelper.getCaseSensitivePath();
    System.out.printf("Original path: %s%n", tPathHelper.getOriginalPath());
    System.out.printf("Case-sensitive path: %s%n", caseSensitivePath);
    System.out.printf("File size: %,d bytes%n", Files.readAllBytes(caseSensitivePath).length);
  }
}
package de.scrum_master.app;

import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.nio.file.TPath;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;

public class TPathHelper {
  private final TPath originalPath;
  private TPath caseSensitivePath;

  public TPathHelper(TPath tPath) {
    originalPath = tPath;
  }

  public TPath getOriginalPath() {
    return originalPath;
  }

  public TPath getCaseSensitivePath() throws IOException, URISyntaxException {
    if (caseSensitivePath != null)
      return caseSensitivePath;
    final TPath absolutePath = new TPath(originalPath.toFile().getCanonicalPath());
    TPath matchingPath = absolutePath.getRoot();
    for (Path subPath : absolutePath) {
      boolean matchFound = false;
      for (TFile candidateFile : matchingPath.toFile().listFiles()) {
        if (candidateFile.getName().equalsIgnoreCase(subPath.toString())) {
          matchFound = true;
          matchingPath = new TPath(matchingPath.toString(), candidateFile.getName());
          break;
        }
      }
      if (!matchFound)
        throw new IOException("element '" + subPath + "' not found in '" + matchingPath + "'");
    }
    caseSensitivePath = matchingPath;
    return caseSensitivePath;
  }
}

当然,这有点难看,如果存档中存在多个不区分大小写的匹配项,那么它将为您提供第一个匹配路径。该算法将在每个子目录中的第一个匹配后停止搜索。我对这个解决方案并不特别自豪,但这是一个很好的练习,你似乎坚持要这样做。我希望你永远不会遇到在区分大小写的文件系统上创建并包含多个可能匹配的UNIX风格的ZIP存档。

BTW,我的示例文件的控制台日志如下所示:

Original path: ..\..\..\downloads\powershellarsenal-master.zip\PowerShellArsenal-master\LIB\CAPSTONE\LIB\X64\LIBCAPSTONE.DLL
Case-sensitive path: C:\Users\Alexander\Downloads\PowerShellArsenal-master.zip\PowerShellArsenal-master\Lib\Capstone\lib\x64\libcapstone.dll
File size: 3.629.294 bytes

答案 1 :(得分:0)

我没有安装 TrueZip ,但我也想知道它在普通Path中是如何工作的,所以我在下面实现了非常相似的@kriegaex解决方案,你可以尝试使用{{1 }}:

caseCheck(path)