与ZipInputStream错误UTFDataFormatException解压文件错误、文件、ZipInputStream、UTFDataFormatException

2023-09-14 00:01:16 作者:指着心脏说ゝ它狠爱你

我有一个错误,而我尝试提取,我已经保存在我的资产文件夹中的.zip的文件,错误出现,因为我的文件有字符N。当我试图让下一个条目的错误抛出: zipIs.getNextEntry()

 私人无效loadzip(字符串的文件夹,为InputStream的InputStream)抛出IOException异常
{
            ZipInputStream zipIs =新ZipInputStream(InputStream的);
            ZipEntry的泽= NULL;

            INT I = 0;
                而((泽= zipIs.getNextEntry())!= NULL){

                    FileOutputStream中FOUT =新的FileOutputStream(文件夹+/+ ze.getName());

                    byte []的缓冲区=新的字节[1024];
                    INT长度= 0;

                    而((长度= zipIs.read(缓冲液))大于0){
                    fout.write(缓冲液,0,长度);
                    }


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


                }
                zipIs.close();
}
 

解决方案:

使用 zip4j

 私人无效loadZip(字符串zipFileName,弦乐目的地)
        {
            ZipFile的zip文件= NULL;
            名单< FileHeader里>标题= NULL;
            尝试 {
                zip文件=新的ZipFile(zipFileName);
                标题= zipFile.getFileHeaders();

            }赶上(抛出:Zip​​Exception E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

            如果(头!= NULL)
            {
                的for(int i = 0; I< headers.size();我++)
                {
                    尝试 {
                        zipFile.extractFile(headers.get(i)中,目的地);

                    }赶上(抛出:Zip​​Exception E){
                        // TODO自动生成的catch块
                        e.printStackTrace();
                    }

                }
            }

        }
 

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

miui文件更新出现错误和updatr.zip放在sd哪个目录

如果您想从资产加载(我的情况),你必须把你的拉链到SD卡文件夹中,之后将其解压缩:

 私人的ArrayList<字符串> moveZipsFromAssets(字符串[] zipsAssets,弦乐目的地)
        {
            ArrayList的<字符串>拉链=新的ArrayList<字符串>();
            的for(int i = 0; I< zipsAssets.length;我++)
            {
                InputStream中的InputStream =的getInputStream(zipsAssets [I]);
                Log.d(标记,+ zipsAssets [I]);
                档案文件=新的文件(目标+/+ zipsAssets [I]);
                尝试 {
                        FileOutputStream中的OutputStream =新的FileOutputStream(文件);

                        INT读= 0;
                        字节[]字节=新字节[1024];

                        而((读= inputStream.read(字节))!= -1){
                            outputStream.write(字节,0,读);
                        }

                        outputStream.close();
                        zips.add(目的地+/+ zipsAssets [I]);
                }赶上(FileNotFoundException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }赶上(IOException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
            }
            回报拉链;
        }
 

例如使用(我们有一个文件夹的资产/ myZipsFolder /哪里都是拉链):`的ArrayList

  zipsExternos = moveZipsFromAssets(getAssets()。表(myZipsFolder),
                    Environment.getExternalStorageDirectory()+/ myZipsFolder);

//并加载拉链:
的for(int i = 0; I< zipsExternos.size();我++)
   loadZip(zipsExternos.get(ⅰ),Environment.getExternalStorageDirectory()+/ myZipsFolder);
 

解决方案

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

编辑,因为 ZipInputStream(InputStream的,字符集)是不是在Android的使用。的

I have an error while I try to extract the files of a .zip that I have stored in my assets folder, the error appear because my file have the character ñ. The error throws when I try to get next entry: 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();
}

SOLUTION

Using 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();
                    }

                }
            }

        }

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

If you want to load from assets (my case), you have to move your zip to sdcard folder and after that extract it:

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;
        }

example use (we have a folder assets/myZipsFolder/ where are all the zips): `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");

解决方案

You need to use a third-party library such as zip4j. Their ZipInputStream implementation supports non-UTF8 file names.

Edited because ZipInputStream(InputStream, Charset)is not available in Android.