首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解压缩android中的子目录文件?

如何解压缩android中的子目录文件?
EN

Stack Overflow用户
提问于 2017-04-28 04:56:27
回答 1查看 886关注 0票数 0

当我解压缩文件时,文件名就像电影\好莱坞蜘蛛侠。实际上,是一个文件夹,是电影中的文件夹,蜘蛛侠是好莱坞的文件。

EN

回答 1

Stack Overflow用户

发布于 2017-04-28 05:39:57

如果电影\好莱坞\蜘蛛侠在创建压缩时是一个文件,那么它应该作为文件提取,不管它是否有扩展名(比如*.mp4,*.flv)。

您可以在名称空间java.util.zip下依赖java,文档链接是这里

编写了一些只提取压缩文件的代码,它应该将文件条目提取为文件(不支持gzip,rar )。

代码语言:javascript
复制
private boolean extractFolder(File destination, File zipFile) throws ZipException, IOException
{
    int BUFFER = 8192;
    File file = zipFile;
    //This can throw ZipException if file is not valid zip archive
    ZipFile zip = new ZipFile(file);
    String newPath = destination.getAbsolutePath() + File.separator + FilenameUtils.removeExtension(zipFile.getName());
    //Create destination directory
    new File(newPath).mkdir();
    Enumeration zipFileEntries = zip.entries();

    //Iterate overall zip file entries
    while (zipFileEntries.hasMoreElements())
    {
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(newPath, currentEntry);
        File destinationParent = destFile.getParentFile();
        //If entry is directory create sub directory on file system
        destinationParent.mkdirs();

        if (!entry.isDirectory())
        {
            //Copy over data into destination file
            BufferedInputStream is = new BufferedInputStream(zip
            .getInputStream(entry));
            int currentByte;
            byte data[] = new byte[BUFFER];
            //orthodox way of copying file data using streams
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    return true;//some error codes etc.
}

该例程不执行任何异常处理,请在驱动程序代码中捕获ZipException和IOException。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43672241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档