0

I try to create a zip file and on-the-fly via NanoHTTPD

That is what I currently have:

    @Override
    public Response serve(IHTTPSession session) {
        String uri = session.getUri();
        if (uri.toString().equals("/test.zip")) {
            try {
                PipedOutputStream outPipe = new PipedOutputStream();
                ZipEncryptOutputStream zeos = new ZipEncryptOutputStream(outPipe, "Foo");
                ZipOutputStream zos = new ZipOutputStream(zeos);

                File file = new File("Test.txt");
                PipedInputStream inPipe = new PipedInputStream(outPipe, 2048);

                ZipEntry ze = new ZipEntry("Test.txt");
                zos.putNextEntry(ze);

                FileInputStream fis = new FileInputStream(file);
                IOUtils.copy(fis, zos);
                fis.close();
                zos.closeEntry();
                zos.close();

                return newChunkedResponse(Response.Status.OK, "application/zip", inPipe);

But when debugging - of course - takes some time, because everything is saved to the ZIP file first.

I guess I have to do the writing to zos somehow in a call back and after wards close the entry and stream? But how?

Is that an efficient way of doing it (I am aiming on Android) ? Creating temp files would be much easier - but it should work on low-end smartphones and also the initial wait of creating the zip file (around 40 MB) should be low when downloading.

Alex
  • 32,506
  • 16
  • 106
  • 171
  • You cannot do things in callbacks as the browser cannot be informed afterwards. You have to handle all synchronous. – greenapps Oct 10 '16 at 15:35
  • I mean a callback from NanoHTTPD which reads the stream and passes it to HTTP stream... – Alex Oct 10 '16 at 15:41
  • But what difference would that make? Not that i understand you.. But how would that speed up things? Which stream would be read by nano? Indead this fidling around with streams looks not to good. But how to do it better? Dont know yet. Instead of writing to a file first you mean. Directly streaming to the browser. – greenapps Oct 10 '16 at 16:15
  • Yes ... I want to start sending to the browser while I am still zipping. Currently it takes some seconds of processing and then the download starts. Also I want to keep memory consumption on the sender as low as possible, so I was thinking "pushing it out" directly makes sense. At least I want to avoid to have the zipped data 2, 3 times in memory. – Alex Oct 10 '16 at 16:21

0 Answers0