0

I want to extract and save an incoming ZipFile from the Server.

This code works only half of the time.

InputStream is = socket.getInputStream();
zipStream = new ZipInputStream(new BufferedInputStream(is);

ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null) {
        String mapPaths = MAPFOLDER + entry.getName();
        Path path = Paths.get(mapPaths);
        if (Files.notExists(path)) {
            fileCreated = (new File(mapPaths)).mkdirs();
        }

        String outpath = mapPaths + "/" + entry.getName();
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(outpath);
            int len = 0;
            while ((len = zipStream.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }
        } finally {
            if (output != null)
                output.close();
        }
    }

I think the problem is, that the method zipStream.getNextEntry() gets called before the data is incoming, how can I wait for incoming data to read?

Oliver
  • 503
  • 1
  • 3
  • 13
  • `This code works only half of the time.` What happens the other half of the time? – copeg Jun 11 '15 at 19:48
  • zipStream.getNextEntry() imediatly returns null and the while-Loop isn't entered... – Oliver Jun 11 '15 at 20:39
  • Are you sure that the other side of the socket is sending the zip-file correctly (flushing it, etc.)? – Stefan Bangels Jun 11 '15 at 20:43
  • Mh I think this works correct, otherwise I couldn't get a correct Zip-File if the download from the receiver works?! – Oliver Jun 11 '15 at 21:18
  • If the loop never is entered, then the problem has to be in the first four lines of code... but they seem correct to me. So the problem has to be with the socket itself. Do you do anything with the socket before reading from it? For example, writing to it? Be aware that If you write to it first, and you close the outputstream, then the complete socket will also be closed (you can no longer read from it). – Stefan Bangels Jun 12 '15 at 05:36
  • I'm just reading From the socket and i don't close it. – Oliver Jun 12 '15 at 09:17

0 Answers0