1

I am sending .tar.gz MultipartFile in my api request, and I want to untar and store all the files in List<File> or List<FileItem>

So Far I have...

 public void importFilesTar(MultipartFile file) throws ContentRepositoryDaoException, Exception {
    List<File> files = new ArrayList<File>();
    List<FileItem> fileItems = new ArrayLis<FileItem>();
    
    InputStream in = file.getInputStream();
    GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);
    TarArchiveEntry entry;
    
    while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
        // Get each file from the .tar.gz, and add it to files (List<File>) or fileItems (List<FileItem>)
    }
 }

But I am unable to successfully untar, get the files and add it to list of File or FileItems. Thanks

user1971376
  • 93
  • 1
  • 4
  • 10
  • Why are you unsuccessful? What's going wrong? Post any errors that you get – Erwin Bolwidt Aug 10 '22 at 00:04
  • @ErwinBolwidt I tried `entry.getFile()` but its not correct based on https://commons.apache.org/proper/commons-compress/apidocs/org/apache/commons/compress/archivers/tar/TarArchiveEntry.html#getFile--, and hence returns `null` I also tried `entry.getName()` which returns a String (the filename basically), but I can store or create a `File` with that. So I am unable to get either `File` or `FileItem` from my above code. – user1971376 Aug 10 '22 at 02:18
  • It doesn't sound like your problem has anything to do with multipart uploads. You would get exactly the same problem if you were reading a .tar.gz file from disk using your code. The javadoc for entry.getFile() says "his method is only useful for entries created from a File or Path but not for entries read from an archive." If you want to access the files in the archive as files on the file system, you need to extract the archive. – Erwin Bolwidt Aug 10 '22 at 04:14

0 Answers0