0

当我尝试解压缩包含带有特殊字符的文件时遇到问题。

假设我有一个带有图像文件的 zip 文件 gallery.zip。

gallery.zip
  - file01.jpg
  - dařbuján.jpg

我的方法开始:

public List<File> unzipToTemporaryFolder(ZipInputStream inputStream)
        throws IOException {
    List<File> files = new LinkedList<File>();
    ZipEntry entry = null;
    int count;
    byte[] buffer = new byte[BUFFER];

    while ((entry = inputStream.getNextEntry()) != null) {

由于捷克字母“ř”和“á”,当我尝试读取文件dařbuján.jpg时,它在 inputStream.getNextEntry() 中失败。它适用于其他文件,例如带有空格的文件(104 25.jpg 或简单的 file.jpg 等)。你能帮我吗?

4

2 回答 2

2

使用指定的字符集创建ZipInputStream

 ZipInputStream(InputStream in, Charset charset)

喜欢

new ZipInputStream(inputStream, Charset.forName("UTF-8"));
于 2013-07-02T11:42:08.733 回答
1

好的,我用commons-compress解决了它。如果有人对此感兴趣,这是我的方法:

public List<File> unzipToTemporaryFolder(ZipInputStream inputStream,
        File tempFile) throws IOException {
    List<File> files = new LinkedList<File>();
    int count;
    byte[] buffer = new byte[BUFFER];

    org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile(tempFile, "UTF-8");
    Enumeration<?> entires = zf.getEntries();
    while(entires.hasMoreElements()) {
        org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry = (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)entires.nextElement();
        if(entry.isDirectory()) {
            unzipDirectoryZipEntry(files, entry);
        } else {            
            InputStream zin = zf.getInputStream(entry);                 

            File temp = File.createTempFile(entry.getName().substring(0, entry.getName().length() - 4) + "-", "." + entry.getName().substring(entry.getName().length() - 3, entry.getName().length()));                                     

            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(temp), BUFFER);
            while ((count = zin.read(buffer, 0, BUFFER)) != -1) {
                outputStream.write(buffer, 0, count);
            }
            outputStream.flush();
            zin.close();
            outputStream.close();
            files.add(temp);            
        }
    }
    zf.close();
    return files;
}
于 2013-07-02T15:27:50.077 回答