Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

android - Extracting file with ZipInputStream error UTFDataFormatException

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");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...