zip4j, extract a password protected file from an inputstream (blob inputstream which is a zip file)

一笑奈何 提交于 2020-04-14 07:48:22

问题


I have a database that contains blobs and a password protected zip inside this database, using the standard File object approach i traditionally see

        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!

my question is, how do i implement something like this without the use of temporary files, and purely obtaining an inputstream of the blob alone, so far i have something like this

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

回答1:


I believe it is not possible via zip4j as it is very centered around files.

Have a look at this one: http://blog.alutam.com/2012/03/31/new-library-for-reading-and-writing-zip-files-in-java/




回答2:


There is a way you can achieve it with the net.lingala.zip4j.io.inputstream.ZipInputStream

(given a byte[] zipFile and a String password)

String zipPassword = "abcabc";

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

then you could loop over your non protected zip

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




回答3:


I faced the same problem while processing a password protected zipped file in a Hadoop File System (HDFS). HDFS doesn't know about the File object.

This is what worked for me using 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();


来源:https://stackoverflow.com/questions/39220815/zip4j-extract-a-password-protected-file-from-an-inputstream-blob-inputstream-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!