Java 使用zipInputStream从服务器下载ZipFile
我想从服务器提取并保存传入的ZipFile 这段代码只有一半的时间有效Java 使用zipInputStream从服务器下载ZipFile,java,download,zip,server,zipinputstream,Java,Download,Zip,Server,Zipinputstream,我想从服务器提取并保存传入的ZipFile 这段代码只有一半的时间有效 InputStream is = socket.getInputStream(); zipStream = new ZipInputStream(new BufferedInputStream(is); ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { String mapPaths = MAPFOLDER + entr
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();
}
}
我认为问题在于,在数据传入之前调用了zipStream.getNextEntry()方法,我如何等待传入数据读取?
此代码只有一半时间有效。
另一半时间会发生什么?zipStream.getNextEntry()I立即返回null,并且未输入while循环…您确定套接字的另一端正确发送了zip文件(刷新它等)?Mh我认为这是正确的,否则如果从接收器下载工作正常,我就无法获得正确的zip文件?!如果循环从未被输入,那么问题必须出现在代码的前四行。。。但在我看来,它们似乎是正确的。因此,问题在于插座本身。在读取插座之前,您是否对其进行了任何操作?比如,给它写信?请注意,如果您先写入它,然后关闭outputstream,那么整个套接字也将关闭(您无法再从中读取)。