wie Spülen Sie den ZipOutputStream regelmäßig in java

Ich versuche, Archiv Liste von Dateien im zip-format und dann herunterladen Sie es für den Benutzer on-the-fly...

Ich bin mit out of memory Problem, wenn das herunterladen einer zip 1 GB Größe

Bitte helft mir, wie kann ich dies beheben, ohne Erhöhung der jvm-heap-Größe. ich möchte zu Spülen, den Bach in regelmäßigen Abständen..

ICH VERSUCHE IN REGELMÄßIGEN ABSTÄNDEN ZU SPÜLEN, ABER DAS IST NICHT FÜR MICH ARBEITEN.

Bitte meinen code unten angehängt:

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) { //flush after 1mb
                    bytesBuffered = 0;
                    zip.flush();

                }
            }
            zip.closeEntry();
            zip.flush();
            fin.close();
        }

    }
}

}
  • gibt es eine Verdichtung in den zipoutputstream? wenn ja, dann kannst du nicht Spülen Sie es.
InformationsquelleAutor Hima Bindu | 2011-08-18
Schreibe einen Kommentar