【问题标题】:Extract contents of Zip file downloaded using a HTTP GET request提取使用 HTTP GET 请求下载的 Zip 文件的内容
【发布时间】:2016-05-02 07:36:40
【问题描述】:

我必须从 rest 调用中下载一个 Zip 文件并提取其内容(一些 PDF 文件和一个 PNG 文件)。

我正在使用 Java Spring。

怎么办?

【问题讨论】:

    标签: java get zip unzip


    【解决方案1】:

    可以使用 Spring 的 RestTemplate 下载文件

    RestTemplate templ = new RestTemplate();
    byte[] downloadedBytes = templ.getForObject(url, byte[].class);
    

    使用标准java或第三方库提取内容。

    示例实用程序,改编自此处:http://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java

    package com.test;
    
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class ZipHelper {
    
        private static final int BUFFER_SIZE = 4096;
    
        public static void unzip(byte[] data, String dirName) throws IOException {
            File destDir = new File(dirName);
            if (!destDir.exists()) {
                destDir.mkdir();
            }
            ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(data));
            ZipEntry entry = zipIn.getNextEntry();
    
            while (entry != null) {
                String filePath = dirName + File.separator + entry.getName();
                if (!entry.isDirectory()) {
                    // if the entry is a file, extracts it
                    extractFile(zipIn, filePath);
                } else {
                    // if the entry is a directory, make the directory
                    File dir = new File(filePath);
                    dir.mkdir();
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }
            zipIn.close();
        }
    
        private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
            byte[] bytesIn = new byte[BUFFER_SIZE];
            int read = 0;
            while ((read = zipIn.read(bytesIn)) != -1) {
                bos.write(bytesIn, 0, read);
            }
            bos.close();
        }
    }
    

    然后你像这样使用这个实用程序:

    ZipHelper.unzip(downloadedBytes, "/path/to/directory");
    

    【讨论】:

    • 谢谢,我需要解码响应吗? r 认为使用的库?
    • 抱歉,“r 认为”是什么意思?
    • 对不起,'or',类似的东西?:Inflater decompressor = new Inflater(); decompressor.setInput(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(file.length);字节[] buf = 新字节[1024]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] decompressedData = bos.toByteArray();
    • 我认为 ZipInputStream 是一种比 Inflator (zlib) 更通用的算法。是否要将 zip 文件解压缩到磁盘上的目录中?
    • @IgorGanapolsky 没有区别。当我们说提取时,我们的意思是解压缩。
    【解决方案2】:

    requestdt 正文应该是这样的:

    @RequestMapping(value = "/push/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public String pushTransactions(@PathVariable("id") String id, @RequestBody byte[] str) throws MessagingException, JsonParseException, JsonMappingException, IOException {
    

    在控制器中,您可以这样做:

    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int len;
            while ((len = gis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
    
            gis.close();
            out.close();
    

    【讨论】:

    • @RequestBody byte[] srt 抛出异常:该位置不允许注释。
    • @FrancescoPerfetti 再次检查我的样品。我已经编辑了
    猜你喜欢
    • 2021-05-21
    • 2013-12-04
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多