2

Is it possible to retrieve a file from a FTP server that is zipped, and unzip it while it is streaming?

This is being run in a cloud environment where I don't have access to the file system so it will be done all in-memory.

I am using commons net ftp client's method: retrieveFileStream

assylias
  • 321,522
  • 82
  • 660
  • 783
cool breeze
  • 4,461
  • 5
  • 38
  • 67
  • What have you try? I guess wrapping your inputstream with a ZipInputStream should work: `new ZipInputStream(retrieveFileStream(...))` – Balázs Nemes Jun 19 '17 at 15:15
  • 1
    Please read this thread, I think it answers your question. https://stackoverflow.com/questions/12030703/uncompressing-a-zip-file-in-memory-in-java – Sudhesh Rajan Jun 19 '17 at 15:26

1 Answers1

3

You are using Apache's common library, and the method you are using returns InputStream, so you can wrap it in ZipArchiveInputStream and read it as you like, as one may argue it's more reliable than the java.util.zip.* solution

If you didn't have access to Apache Commons, you could use ZipInputStream to wrap your source InputStream.

vadkou
  • 390
  • 2
  • 9
  • So the Zip input stream takes care of the tokening etc. i.e. it knows when an entire row of the file has been streamed? i.e. i want to loop through each line of the file and parse it. – cool breeze Jun 19 '17 at 15:23
  • @coolbreeze It only takes care of reading between EndOfFile markers. To parse file line by line you can read the file into buffer until End Of Line is encountered. Platform-independent EOL is represented as System.lineSeparator() – vadkou Jun 19 '17 at 15:26