Download ZipFile from server using zipInputStream

841 views Asked by At

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?

0

There are 0 answers