3

当我尝试提取存储在资产文件夹中的 .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/ 所有的 zip 都在哪里):`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");
4

1 回答 1

4

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

已编辑,因为ZipInputStream(InputStream, Charset)在 Android 中不可用。

于 2014-07-01T12:21:44.027 回答