0

I am trying to convert a zip file into a File object on java. I need to create a method which accepts a zip file and return an unzipped file back. I do not want to write the physical file anywhere in hard disc (not even a temporary file) . Is it possible?

File convert(File zipFile) {
  //do conversion without physical file
  return unzip file;
}

I have tried https://www.baeldung.com/java-compress-and-uncompress and able to unzip but it is writing into a physical file only.

User_1940878
  • 321
  • 1
  • 6
  • 25
  • Does this answer your question? [java opening zip files into memory](https://stackoverflow.com/questions/33320064/java-opening-zip-files-into-memory) – Federico klez Culloca Jul 19 '22 at 11:42
  • And then [this](https://stackoverflow.com/questions/357851/in-java-how-to-zip-file-from-byte-array) to zip it back. – Federico klez Culloca Jul 19 '22 at 11:44
  • @Federico klez Culloca at tis point, I do not want to read the file content. Just a utility method to convert zip to file object. The input must be type File – User_1940878 Jul 19 '22 at 12:14
  • 1
    I don't think that's possible. Consider that a `File` is ["\[a\]n abstract representation of file and directory pathnames"](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html), so it doesn't really represent the more general abstract concept of a file. In other words, why do you need to return a `File`? – Federico klez Culloca Jul 19 '22 at 12:29
  • Because I am modifying a part of the code which is already running in production. The existing method deals with File object (input parameter File object) and now instead of normal file, the method may get Zip file as well. So in case of Zip file, I need to unzip it and supply to the existing method – User_1940878 Jul 19 '22 at 12:44
  • I don't think you can do that with the `File` API, you would have to write a temporay file. It can be done using the new style `Path` and `FileSystem` APIs, but that won't help if it must be `File`. – greg-449 Jul 19 '22 at 12:53
  • An option would be to create a ramdisk and unzip the file in there if you don't want it on disk – Federico klez Culloca Jul 19 '22 at 12:57

0 Answers0