2

我有一个数据库,其中包含 blob 和该数据库内的受密码保护的 zip,使用我传统上看到的标准文件对象方法

        File zipFile = new File("C:\\file.zip");        
        net.lingala.zip4j.core.ZipFile table = new net.lingala.zip4j.core.ZipFile(zipFile);                    
        if (table.isEncrypted())
            table.setPassword(password);           

        net.lingala.zip4j.model.FileHeader entry = table.getFileHeader("file_inside_the_zip.txt");
        return table.getInputStream(entry); //Decrypted inputsteam!

我的问题是,我如何在不使用临时文件的情况下实现这样的东西,并且只获得 blob 的输入流,到目前为止我有这样的东西

InputStream zipStream = getFileFromDataBase("stuff.zip");
//This point forward I have to save zipStream as a temporary file and use the traditional code above
4

4 回答 4

0

我相信通过 zip4j 是不可能的,因为它非常以文件为中心。

看看这个:http ://blog.alutam.com/2012/03/31/new-library-for-reading-and-writing-zip-files-in-java/

于 2016-10-10T14:43:49.813 回答
0

有一种方法可以通过 net.lingala.zip4j.io.inputstream.ZipInputStream 实现

(给定一个 byte[] zipFile 和一个字符串密码)

String zipPassword = "abcabc";

ZipInputStream innerZip = new ZipInputStream(new ByteArrayInputStream(zipFile), zipPassword.toCharArray());

然后你可以循环你的非保护拉链

File zip = null;
while ((zipEntry = zipIs.getNextEntry()) != null) {
  zip = new File(file.getAbsolutePath(), zipEntry.getFileName());
  ....
}

于 2020-03-12T16:04:41.910 回答
0

在 Hadoop 文件系统 (HDFS) 中处理受密码保护的压缩文件时,我遇到了同样的问题。HDFS 不知道 File 对象。

这就是使用 zip4j 对我有用的方法:

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path hdfsReadPath = new Path(zipFilePath); // like "hdfs://master/dir/sub/data/the.zip"

        FSDataInputStream inStream =   fs.open(hdfsReadPath);
        ZipInputStream zipInputStream = new ZipInputStream(inStream, passWord.toCharArray());

        LocalFileHeader zipEntry = null;

        BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream));
        while ((zipEntry = zipInputStream.getNextEntry()) != null ) {
            String entryName = zipEntry.getFileName();
            System.out.println(entryName);
            if (!zipEntry.isDirectory()) {
                String line;
                while ((line = reader.readLine()) != null) {
                    //process the line
                }
            }
        }
        reader.close();
        zipInputStream.close();
于 2020-04-10T10:38:56.173 回答
0
public void extractWithZipInputStream(File zipFile, char[] password) throws IOException {
    LocalFileHeader localFileHeader;
    int readLen;
    byte[] readBuffer = new byte[4096];

    InputStream inputStream = new FileInputStream(zipFile);
    try (ZipInputStream zipInputStream = new ZipInputStream(inputStream, password)) {
      while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
        File extractedFile = new File(localFileHeader.getFileName());
        try (OutputStream outputStream = new FileOutputStream(extractedFile)) {
          while ((readLen = zipInputStream.read(readBuffer)) != -1) {
            outputStream.write(readBuffer, 0, readLen);
          }
        }
      }
    }
  }

此方法需要根据您的需要进行修改。例如,您可能必须更改输出位置。我已经尝试过了,它奏效了。为了更好地理解,请参阅https://github.com/srikanth-lingala/zip4j

于 2020-07-09T12:24:22.500 回答