如何使用流在Java中生成ZipFile

问题描述 投票:0回答:2

我不熟悉使用流。我尝试在应用程序中使用httpClient下载zipfil。当我下载zip文件时,我从客户端获得了成功的完整响应,但在zipfile中看不到任何内容。任何人都可以帮助我。下面是我的代码。

   String url = CONTANTS.SERVER_LOCATION+"Download/Test/"+ "sudhakar_test.zip";
   HttpGet getRequest = new HttpGet(url);
   CloseableHttpClient httpClient =HttpClientBuilder.create().build();
   try (
          CloseableHttpResponse response = httpClient.execute(getRequest);
          InputStream fileInput = response.getEntity().getContent();
          ZipInputStream zipInput = new ZipInputStream (fileInput);
          //Output stream to write file from SPOS to
          FileOutputStream output = new FileOutputStream(new File(fileLocation 
          +"/SudhakarZIP_Test.zip"));
        )
          {
            if (response.getStatusLine().getStatusCode() == 200)
             {          
              byte[] buffer = new byte[1024];
              int length;
              while((length = zipInput.read(buffer,0,buffer.length)) >0)
               {
                    output.write(buffer,0,length);
               }
        } 
java webservice-client fileinputstream fileoutputstream zipinputstream
2个回答
0
投票

您至少需要从ZIP流中调用getNextEntry以获得任何有意义的内容,因为ZipInputStream正在读取整个存档,而不是单个文件。如果您不读取条目,则流可能不会返回任何字节。


0
投票

Maarten是正确的尝试此代码:

© www.soinside.com 2019 - 2024. All rights reserved.