[英]How to generate the ZipFile in java
我是使用流的新手。我试图在我的应用程序中使用 httpClient 下载 zipfile。当我下载 zip 文件时,我收到了来自客户端的成功响应。响应我试图在我的本地生成 zipfile使用 inputstream 的文件夹。我能够生成 zip 文件,但没有内容。有人可以在这里帮助我。 下面是我的代码片段。
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);
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);
}
}
您至少需要从 ZIP 流中调用getNextEntry
来获取任何有意义的内容,因为ZipInputStream
正在读取整个存档,而不是单个文件。 如果您没有阅读条目,则该流可能不会返回任何字节。
您可以解压缩流,也可以将其直接写入文件,在这种情况下,您根本不需要ZipInputStream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.