重新创建目录结构并从Java

时间:2015-12-27 11:02:44

标签: java file zip

我有一个zip文件的字节数组,我需要从中创建zip文件,然后解压缩其中的文件和文件夹。

我尝试这样做,但让zip条目为空

{
ResponseEntity<byte[]> result = restTemplate.exchange(builder.build().toString(), HttpMethod.GET, httpEntity, byte[].class);
    ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(result.getBody()))
    ZipEntry entry = null;
    while ((entry = zipStream.getNextEntry()) != null)
}

Zip文件就像

  • DIR - &gt; b.txt
  • DIR - &gt; x.php,a.txt

先谢谢

1 个答案:

答案 0 :(得分:3)

好的,我成功地以编程方式下载了上述链接的文件:

        List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
        acceptableMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(acceptableMediaTypes);
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> httpEntity = new HttpEntity<String>(headers);
        ResponseEntity<byte[]> result = restTemplate.exchange( "someurl deleted On purpose",
                                                               HttpMethod.GET,
                                                               httpEntity,
                                                               byte[].class );

        ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(result.getBody()));
        ZipEntry entry = null;
        while ((entry = zipStream.getNextEntry()) != null)
        {
            System.out.println( "entry: " + entry );
        }

输出结果为:

entry: bridge2cart/
entry: bridge2cart/config.php
entry: bridge2cart/bridge.php
entry: readme.txt

因此,您可能无法请求正确的媒体类型,或者URL未正确编码。