首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中解压非UTF8格式的文件

如何在java中解压非UTF8格式的文件
EN

Stack Overflow用户
提问于 2012-07-31 14:03:32
回答 5查看 7.5K关注 0票数 4

我有一个文件,例如test.zip。如果我使用像winrar这样的压缩工具,它很容易解压(将test.zip解压到test.csv)。但是test.csv不是UTF8格式。我这里的问题是,当我使用java解压它时,它不能读取这个文件。

代码语言:javascript
复制
ZipFile zf = new ZipFile("C:/test.zip");

抛出的异常表明打开该文件时会发生错误。

在java上,http://java.sun.com/developer/technicalArticles/Programming/compression/并没有写任何关于数据格式化的东西。也许整个API只针对UTF8格式的数据而设计。那么,如果我必须解压除UTF8格式以外的数据,如何解压?尤其是包含较大空间大小的日文和中文字符(UTF8除外)。我还在http://truezip.java.net/6/tutorial.html上找到了一个API,其中提到了这个问题。但是,我没有办法解决这个问题。有什么简单的方法可以解决这个问题吗?尤其是从JAVA规范请求传递的API。

EN

回答 5

Stack Overflow用户

发布于 2013-12-11 23:39:38

JDK6在java.util.zip实现中有一个错误,它无法处理非USASCII码字符。我使用Apache Commons commons-compress-1.0.jar库来修复它。JDK7已修复java.util.zip实现。http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html

代码语言:javascript
复制
import java.io.*;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.*;

public static int unzip(File inputZip, File outputFolder) throws IOException {
    int count=0;
    FileInputStream fis = null;
    ZipArchiveInputStream zis = null;
    FileOutputStream fos = null;
    try {
        byte[] buffer = new byte[8192];
        fis = new FileInputStream(inputZip);
        zis = new ZipArchiveInputStream(fis, "Cp1252", true); // this supports non-USACII names
        ArchiveEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            File file = new File(outputFolder, entry.getName());
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                count++;
                file.getParentFile().mkdirs();
                fos = new FileOutputStream(file);
                int read;
                while ((read = zis.read(buffer,0,buffer.length)) != -1)
                    fos.write(buffer,0,read);
                fos.close();
                fos=null;
            }
        }
    } finally {
        try { zis.close(); } catch (Exception e) { }
        try { fis.close(); } catch (Exception e) { }
        try { if (fos!=null) fos.close(); } catch (Exception e) { }
    }
    return count;
}
票数 4
EN

Stack Overflow用户

发布于 2012-07-31 14:06:29

不,zip文件不仅仅是用于UTF-8数据的。Zip文件根本不会尝试解释文件中的数据,Java API也不会。

文件的非ASCII名称可能会有问题,但文件内容本身应该不是问题。在您的例子中,文件的名称看起来只是test.zip,所以您应该不会遇到任何名称编码问题。

如果文件无法打开,那么听起来就像你遇到了另一个问题。您确定文件存在于您期望的位置吗?

票数 3
EN

Stack Overflow用户

发布于 2012-07-31 19:14:42

你能试试下面的代码吗?有关更多示例,请查看此处http://java2novice.com/java-collections-and-util/zip/unzip/

代码语言:javascript
复制
FileInputStream fis = null;
    ZipInputStream zipIs = null;
    ZipEntry zEntry = null;
    try {
        fis = new FileInputStream(filePath);
        zipIs = new ZipInputStream(new BufferedInputStream(fis));
        while((zEntry = zipIs.getNextEntry()) != null){
            try{
                byte[] tmp = new byte[4*1024];
                FileOutputStream fos = null;
                String opFilePath = "C:/"+zEntry.getName();
                System.out.println("Extracting file to "+opFilePath);
                fos = new FileOutputStream(opFilePath);
                int size = 0;
                while((size = zipIs.read(tmp)) != -1){
                    fos.write(tmp, 0 , size);
                }
                fos.flush();
                fos.close();
            } catch(Exception ex){

            }
        }
        zipIs.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11734084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档