压缩文件以byte[]型从数据库读出 ,如何能利用java.util.zip 直接获得压缩文件内容(多张Image图片),而不需要在本地生成一个(*.zip)文件。即 byte[]  ------> List<Image>我现在的做法在 byte[]转成压缩文件时,总是在本地生成一个*.zip 文件,我希望能在缓存中实现,能否做到。反过来也是 List<Image> ------> byte[]  

解决方案 »

  1.   

    只会做前一半,即 将数据库读出的byte[]转换成ZipInputStream来解压缩, // 将byte[]转换成InputStream流
    InputStream in = new ByteArrayInputStream(buf);
    // 读取 ZIP 文件格式的文件输入流
    ZipInputStream zipInputStream = new ZipInputStream(in);但后一半不会做,还是要在本地生成解压后的文件,再想想
    /**
     * 解压缩
     * @param buf 数据库读出的byte[]
     */
    public void unzip(byte[] buf) {
    try {
    // 压缩文件
    File file = new File("随意");
    // 实例化ZipFile,每一个zip压缩文件都可以表示为一个ZipFile
    ZipFile zipFile = new ZipFile(file);
    // 将byte[]转换成InputStream流
    InputStream in = new ByteArrayInputStream(buf);
    // 读取 ZIP 文件格式的文件输入流
    ZipInputStream zipInputStream = new ZipInputStream(in);
    // 用于表示 ZIP 文件条目
    ZipEntry zipEntry = null;
    while((zipEntry = zipInputStream.getNextEntry()) != null) {
    String fileName = zipEntry.getName();// 返回条目名称
    File temp = new File("D:\\unpackTest\\" + fileName);
    if (!temp.getParentFile().exists())
    temp.getParentFile().mkdirs();
    OutputStream os = new FileOutputStream(temp);
    // 通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
    InputStream is = zipFile.getInputStream(zipEntry);
    int len = 0;
    while((len = is.read()) != -1)
    os.write(len);
    os.close();
    is.close();
    }
    zipInputStream.close();
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }