使用ZipInputStream错误UTFDataFormatException提取文件

时间:2014-07-01 11:33:35

标签: android zipinputstream

我尝试提取存储在我的资源文件夹中的.zip文件时出错,出现错误是因为我的文件有字符ñ。当我尝试下一个条目时,错误抛出:zipIs.getNextEntry()

private void loadzip(String folder, InputStream inputStream) throws IOException
{
            ZipInputStream zipIs = new ZipInputStream(inputStream); 
            ZipEntry ze = null;

            int i=0;
                while ((ze = zipIs.getNextEntry()) != null) {

                    FileOutputStream fout = new FileOutputStream(folder +"/"+ ze.getName());

                    byte[] buffer = new byte[1024];
                    int length = 0;

                    while ((length = zipIs.read(buffer))>0) {
                    fout.write(buffer, 0, length);
                    }


                    zipIs.closeEntry();
                    fout.close();


                }
                zipIs.close();
}

使用zip4j

private void loadZip(String zipFileName, String destination)
        {
            ZipFile zipFile = null;
            List<FileHeader> headers = null;
            try {
                zipFile = new ZipFile(zipFileName);
                headers = zipFile.getFileHeaders();

            } catch (ZipException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if(headers != null)
            {
                for(int i=0;i<headers.size();i++)
                {
                    try {
                        zipFile.extractFile(headers.get(i),destination);

                    } catch (ZipException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }

        }

............................................... .................................................. ................................

如果你想从资产(我的情况)加载,你必须将你的zip移动到sdcard文件夹,然后提取它:

private ArrayList<String> moveZipsFromAssets(String[] zipsAssets, String destination)
        {
            ArrayList<String> zips = new ArrayList<String>();
            for(int i=0;i<zipsAssets.length;i++)
            {
                InputStream inputStream = getInputStream(zipsAssets[i]);
                Log.d(tag, ""+zipsAssets[i]);
                File file = new File(destination+"/"+zipsAssets[i]);
                try {
                        FileOutputStream outputStream = new FileOutputStream(file);

                        int read = 0;
                        byte[] bytes = new byte[1024];

                        while ((read = inputStream.read(bytes)) != -1) {
                            outputStream.write(bytes, 0, read);
                        }

                        outputStream.close();
                        zips.add(destination+"/"+zipsAssets[i]);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return zips;
        }

示例使用(我们有一个文件夹assets / myZipsFolder /其中都是拉链):`ArrayList

zipsExternos = moveZipsFromAssets(getAssets().list("myZipsFolder"),
                    Environment.getExternalStorageDirectory()+"/myZipsFolder");

//and load the zips:
for(int i=0;i<zipsExternos.size();i++)
   loadZip(zipsExternos.get(i),Environment.getExternalStorageDirectory()+"/myZipsFolder");

1 个答案:

答案 0 :(得分:4)

您需要使用第三方库,例如zip4j。他们的ZipInputStream实现支持非UTF8文件名。

已修改,因为Android中没有ZipInputStream(InputStream, Charset)