I am trying to archive a list of files in zip format and then upload it for a user on the fly ...
I have a memory problem while loading a zip of 1gb size
Please help me how can I solve this problem without increasing the jvm heap size. I would like to periodically flush the stream.
I NOW TO REPEAT PERIODICALLY, BUT THIS DOES NOT WORK FOR ME.
Please find my code below:
try{ ServletOutputStream out = response.getOutputStream(); ZipOutputStream zip = new ZipOutputStream(out); response.setContentType("application/octet-stream"); response.addHeader("Content-Disposition", "attachment; filename=\"ResultFiles.zip\""); //adding multiple files to zip ZipUtility.addFileToZip("c:\\a", "print1.txt", zip); ZipUtility.addFileToZip("c:\\a", "print2.txt", zip); ZipUtility.addFileToZip("c:\\a", "print3.txt", zip); ZipUtility.addFileToZip("c:\\a", "print4.txt", zip); zip.flush(); zip.close(); out.close(); } catch (ZipException ex) { System.out.println("zip exception"); } catch (Exception ex) { System.out.println("exception"); ex.printStackTrace(); }
public class ZipUtility { static public void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception { File file = new File(path + "\\" + srcFile); boolean exists = file.exists(); if (exists) { long fileSize = file.length(); int buffersize = (int) fileSize; byte[] buf = new byte[buffersize]; int len; FileInputStream fin = new FileInputStream(path + "\\" + srcFile); zip.putNextEntry(new ZipEntry(srcFile)); int bytesread = 0, bytesBuffered = 0; while ((bytesread = fin.read(buf)) > -1) { zip.write(buf, 0, bytesread); bytesBuffered += bytesread; if (bytesBuffered > 1024 * 1024) {
source share