如何使用Storage Volume(/ StorageAccessFramework)解压缩文件?

时间:2018-05-14 11:55:51

标签: android android-intent zip unzip storage-access-framework

我使用此代码(Android 7.0 / Nougat)将zip文件解压缩到外部存储空间(包括多个文件夹级别):

try {
    ZipFile zip = new ZipFile(zippath);
    Enumeration enu = zip.entries();

    while(enu.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enu.nextElement();
        BufferedInputStream bis = null;
        String fileName = null;

        try {
            fileName = zipEntry.getName();
            fileName = fileName.replace("\\",File.separator).replace("/",File.separator);
            int p = fileName.lastIndexOf(File.separator);

            if(p>=0) {
                File fd=new File(folderpath+File.separator+fileName.substring(0,p));
                fd.mkdirs();
            }

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(folderpath+File.separator+fileName));
            bis = new BufferedInputStream(zip.getInputStream(zipEntry));
            byte[] buffer = new byte[10000];
            int len = 0;

            while ((len = bis.read(buffer, 0, 10000)) > 0) {
                bos.write(buffer, 0, len);
            }

            bis.close();
            bos.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }
    }
} catch (IOException e2) {
    e2.printStackTrace();
}

要使用createAccessIntent(存储卷)使用DocumentFile而不是普通File来获取对SD卡的写入权限。

我已经这样做了ZipInputStream

InputStream inputStream = this.getContentResolver().openInputStream(myDocumentFileZip.getUri());
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream);
ZipEntry zipEntry;

...而且我猜你会继续这样:

while ((zipEntry = zipInputStream.getNextEntry()) != null) {

但是你从那里做了什么 - 如何将文件复制到SD卡上并保持文件夹结构,如上面的代码,但使用什么存储卷(或存储访问框架)提供?

1 个答案:

答案 0 :(得分:0)

解压缩存储卷:

小心:这样,如果你多次解压缩相同的.zip文件,它会创建副本,而我在第一篇文章中的原始代码(你不能用于SD卡)不会自动覆盖! / p>

try {
    InputStream is = getContentResolver().openInputStream(myZip.getUri());
    BufferedInputStream bis = new BufferedInputStream(is);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry zipEntry;

    while ((zipEntry = zis.getNextEntry()) != null) {
        String fileName = null; 

        try {
            fileName = zipEntry.getName();        
            fileName = fileName.replace("\\",File.separator).replace("/",File.separator);
            int p=fileName.lastIndexOf(File.separator);        
            DocumentFile destFolder = myDestFolder; //DocumentFile of the destination folder
            String destName = fileName;

            if (p>=0) {
                String[] split = fileName.split(File.separator);

                //If the .zip file contains multiple folder levels, this is where you  
                //have to check and then create them, e.g. for 3 levels:
                if(split.length==1) {
                    destFolder = myFolder;
                    destName = filename;
                } else if(split.length==2) {
                    if(mySubFolder==null) {
                        mySubFolder = myFolder.createDirectory(split[0]);
                    }

                    destFolder = mySubFolder;
                    destName = split[1];
                } else if(split.length==3) {
                    if(mySubFolder==null) {
                        mySubFolder = myFolder.createDirectory(split[0]);
                    }
                    if(mySubSubFolder==null) {
                        mySubSubFolder = mySubFolder.createDirectory(split[1]);
                    }

                    destFolder = mySubSubFolder;
                    destName = split[2];
                }
            }

            DocumentFile df = null;

            //Now you have to tell it what file extensions ("MIME" type) you want to use, e.g.:
            if(destName.endsWith(".txt")) {
                df = destFolder.createFile("text/plain",destName.substring(0,destName.length()-4));
            } else if(destName.endsWith(".jpg")) {
                df = destFolder.createFile("image/jpeg",destName.substring(0,destName.length()-4));
            }

            OutputStream out = getContentResolver().openOutputStream(df.getUri());
            BufferedOutputStream bos = new BufferedOutputStream(out);
            long zipfilesize = zipEntry.getSize();

            byte[] buffer = new byte[10000];
            int len = 0;
            int totlen = 0;

            while (((len = zis.read(buffer, 0, 10000)) > 0) && (totlen < zipfilesize)) {
                bos.write(buffer, 0, len);
                totlen += len;
            }

            bos.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }
    }

    is.close();
    bis.close();
    zis.close();
} catch (IOException e2) {
    e2.printStackTrace();
}

修改:重要java.util.zip未设置sizecompressedSize(将返回“-1”),这就是为什么此代码只会使用由库创建的zip文件创建大小为0B的文件 - 手工创建的zip文件(例如使用WinRar)可以正常工作。要解决此问题,请替换

while (((len = zis.read(buffer, 0, 10000)) > 0) && (totlen < zipfilesize)) {

while (((len = zis.read(buffer, 0, 10000)) > 0)) {

可以这样做是因为:

  

对ZipInputStream.getNextEntry()的调用将InputStream定位在条目的开头,因此提供ZipInputStream相当于提供ZipEntry的InputStream。

来源:https://stackoverflow.com/a/3233600/2016165

这个的缺点(与我的非StorageVolume版本相比)是你a)无法获得zip中的文件总数而b)也无法获得文件的(总)大小,这意味着您无法在“解压缩...”对话框中设置进度条,除非您首先遍历所有zip条目以计算它们。