1

如何在 zip 存档中读取二进制文件(获取字节数组)?我正在使用TrueZip

我的例子:

import de.schlichtherle.truezip.file.TFile;
import java.util.HashMap;

public class Archive {
     private final TFile archive;
     // String in HashMap represents filename ( eg. MyTextFile.txt )
     private final HashMap<String, TFile> entries = new HashMap<>();

public Archive( String path ) {
    this.archive = new TFile(path);
    this.listEntries( archive.getAbsolutePath() );
}

// lists all files
private void listEntries( String pathToZipFile ) {
    TFile archive = new TFile( pathToZipFile );
    for ( TFile listFile : archive.listFiles() ) {
        if ( listFile.isDirectory() ) {
            listEntries(listFile.getAbsolutePath());
        } else {
            entries.put(listFile.getName(), listFile);
        }
    }
}

public byte[] getBytes( String key ) {
     TFile file = entries.get(key);
     // ...

}

我正在寻找这样的东西,但对于 TFile / TPath:

Files.readAllBytes( file.toPath() );
4

1 回答 1

1

有一个 TPath 类 ( https://truezip.java.net/truezip-path/ ) 和 NIO.2 文件系统的适配器,所以它非常简单:

Path path = new TPath(new URI("http://acme.com/download/everything.zip/README.TXT"));
byte[] bytes= Files.readAllBytes(path);
于 2017-03-03T22:39:39.040 回答