【问题标题】:Android unzip zip file from internal storageAndroid 从内部存储中解压缩 zip 文件
【发布时间】:2017-09-28 14:52:01
【问题描述】:

我正在将 zip 文件从服务器下载到内部存储。下载后,我想将该文件解压缩到内部存储中。我的文件已下载到内部存储,我可以看到,但解压缩时我无法读取它。下面是我的 unzipfile() 方法。谁能告诉我哪里出错了?

public void unzipfile()
    {
        try {

            Log.d("unzipiing","unzipping");
            ContextWrapper cw = new ContextWrapper(context);
            String name_="foldername"; //Folder name in device android/data/
            File directory = cw.getDir(name_, Context.MODE_PRIVATE);
            File mypath=new File(directory,"dwnld");

            FileOutputStream fout = new FileOutputStream(mypath);

            File yourFile = new File(directory,"dwnld.zip");
            Log.d("unzipiing","filepath -" + yourFile.getPath());

            FileInputStream fin = new FileInputStream(yourFile.getPath());
            ZipInputStream zin = new ZipInputStream(fin);
            Log.d("unzipiing","zin size -" + zin.available());
           // zin.available() give -1 in console log

            BufferedInputStream in = new BufferedInputStream(zin);
            BufferedOutputStream out = new BufferedOutputStream(fout);


        byte b[] = new byte[zin.available()];
        int n;
            Log.d("unzip","n - " + in.read(b,0,1024));
        while ((n = in.read(b,0,1024)) >= 0) {
            out.write(b,0,n);
            Log.d("unzip byte"," - " + n);
        }

        out.flush();
        out.close();
            in.close();
            fin.close();
            zin.close();
        }
        catch (Exception e){
        }
    }

【问题讨论】:

  • 问题到底出在哪里还不清楚。您可以尝试其他解决方案来完成它:stackoverflow.com/questions/3382996/…
  • 感谢您的链接和帮助。将尝试该解决方案。但我想知道为什么这段代码不起作用:-(
  • 您可以暂时评论您的代码,继续使用下一组解决方案,当您有空时尝试调试您的代码。
  • 您应该在多个地方使用 File#exists() 检查文件或目录是否存在。此外,catch 块中没有代码,因此您不知道会发生什么。谁下载了文件?走哪条路?告诉我们所有完整路径。

标签: android storage unzip internal


【解决方案1】:
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

使用此方法解压

 public void unzip(String _zipFile, String _targetLocation) {

            //create target location folder if not exist
            dirChecker(_targetLocatioan);

            try {
                FileInputStream fin = new FileInputStream(_zipFile);
                ZipInputStream zin = new ZipInputStream(fin);
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {

                    //create dir if required while unzipping
                    if (ze.isDirectory()) {
                        dirChecker(ze.getName());
                    } else {
                        FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
                        for (int c = zin.read(); c != -1; c = zin.read()) {
                            fout.write(c);
                        }

                        zin.closeEntry();
                        fout.close();
                    }

                }
                zin.close();
            } catch (Exception e) {
                System.out.println(e);
            }
    }

检查路径

public void dirChecker(String filepath)
{
File file = new File(filePath);
if(file.exists())      
//Do something
else
// Do something else.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多