Java 解压缩ZipInputStream时出现NegativeArraySizeException

Java 解压缩ZipInputStream时出现NegativeArraySizeException,java,file,exception,client-server,Java,File,Exception,Client Server,我尝试使用ZipInputStream/ZipOutputStream将一些文件从服务器发送到客户端 在服务器上,一切都进行得很顺利,但在客户端,当我想要解压缩它时,文件的大小是-1 所以它失败了。我该怎么做?为什么会这样 socket = new Socket("127.0.0.1",3000); String outDir = "C:\\here"; BufferedInputStream bis = new Bu

我尝试使用ZipInputStream/ZipOutputStream将一些文件从服务器发送到客户端

在服务器上,一切都进行得很顺利,但在客户端,当我想要解压缩它时,文件的大小是-1

所以它失败了。我该怎么做?为什么会这样

             socket = new Socket("127.0.0.1",3000);

         String outDir = "C:\\here";



             BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());            



            ZipInputStream zips = new ZipInputStream(bis);
            ZipEntry zipEntry = null;




              while(null != (zipEntry = zips.getNextEntry())){
             String fileName = zipEntry.getName();
                File outFile = new File(outDir + "/" + fileName);
        System.out.println(outFile.getName()+"      "+zipEntry.getCompressedSize());

    if(zipEntry.isDirectory()){
    File zipEntryFolder = new File(zipEntry.getName());
    if(zipEntryFolder.exists() == false){
     outFile.mkdirs();

    }

        continue;
    }else{
    File parentFolder = outFile.getParentFile();
    if(parentFolder.exists() == false){
     parentFolder.mkdirs();
    }
   }

   System.out.println("ZipEntry::"+zipEntry.getCompressedSize());



        FileWriter fW=new FileWriter(outFile);
            try (BufferedWriter bfW = new BufferedWriter(fW)) {
                bfW.write(zips.getNextEntry().toString());
            }
                                                                    }

              socket.close();

                                                            }
zipEntry.getCompressedSize()的结果;等于-1。但在将其写入服务器中的套接字之后,我立即检查大小,它是实际大小。所以我感到困惑


IDE给出的例外是客户端无效存储块长度中的错误

您永远不能依赖
getSize
返回实际的未压缩大小。您需要从ZipInputStream调用
zips.read
,直到它不再读取任何字节。

@JqueryLearner为什么为null?它已经充满了nextery如果你仔细阅读代码现在我把整个代码,包括整个过程,它会给出一个错误。我知道如果从Long缩小到int可能会导致符号的变化,但在我前面提到的那行,我没有做任何铸造,但仍然给出-1,它将始终返回-1。您不能指望它返回大小。它有时返回大小,但大多数时候返回-1。文件甚至这样说。(好吧,它并没有说它大部分时间都会发生,但它确实说它会发生)zipEntry.getCompressedSize()呢;?因为它也给出了-1,IDE在客户端无效存储块长度中给出了一个异常错误。我不知道getCompressedSize,但我认为您需要未压缩的大小。但在解压缩之前,我首先尝试查看大小,因为IDE给出了异常,这是客户端无效存储块长度中的错误