4

I am trying to create a zip file with collection of files. I used java.nio.Files class to do this:

Files.copy( toBeAdded,internalTargetPath, 
            StandardCopyOption.REPLACE_EXISTING );

It is running out of heap memory due to file size is around 670MB, so is their anyway to do copy with chunks.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JAVAC
  • 1,230
  • 3
  • 17
  • 38
  • found a solution? – Fabio Ebner Jan 29 '18 at 20:07
  • it depends on the destination stream, for instance for HTTP streams you need to set a parameter to avoid storing the full file in memory https://stackoverflow.com/questions/2082057/outputstream-outofmemoryerror-when-sending-http – csanchez Apr 09 '18 at 14:47

1 Answers1

0

You can include commons-io, specifcally, the copyLarge method does the trick:

FileOutputStream internalTargetPathStream = new FileOutputStream(internalTargetPath); long size = IOUtils.copyLarge(sourceStream, internalTargetPathStream);

hd1
  • 33,938
  • 5
  • 80
  • 91