3

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
jyonkheel
  • 443
  • 4
  • 17
  • Did you solve it? I have a similar problem - wanting to extract getContentResolver().openInputStream(uri). – Alex Oct 10 '16 at 14:23
  • I doubt it is possible at the moment ... looking at `net.lingala.zip4j.core.ZipFile#readZipInfo` for example, this is purely based on files, not streams. But as Zip4J is open source, it can be extended ... but might be some work. – Alex Oct 10 '16 at 14:30

4 Answers4

1

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();
0

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/

Alex
  • 32,506
  • 16
  • 106
  • 171
0

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());
  ....
}

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);
          }
        }
      }
    }
  }

This method needs to be modified according to your need. For example, you may have to change the output location. I have tried and it has worked. For better understanding see https://github.com/srikanth-lingala/zip4j

ZeroOneZeroR
  • 667
  • 1
  • 7
  • 12