首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在android中压缩文件夹来制作docx文件?

如何在android中压缩文件夹来制作docx文件?
EN

Stack Overflow用户
提问于 2012-11-26 22:04:23
回答 2查看 926关注 0票数 2

我正在尝试制作一个Android应用程序,可以打开docx文件进行读取、编辑和保存。

我的想法是将存档中的所有xml文件提取到一个临时文件夹中。在这个文件夹中,我们可以在/word/document.xml中编辑文档的内容。问题是,当我压缩这个临时文件夹来创建一个新的docx文件并替换旧文件时,在新的docx存档中,路径类似于/mnt/sdcard/temp/"all files xml go here",而xml文件应该在第一级。

有人能帮我解决这个问题吗?以下是压缩临时目录的方法

注意:我使用的dir2zip参数的值是/mnt/sdcard/temp/***.docx

代码语言:javascript
运行
复制
public void zipDir(String dir2zip, ZipOutputStream zos)
{
    try
   {
        //create a new File object based on the directory we
        //have to zip File   
        File zipDir = new File(dir2zip);

        //get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;

        //loop through dirList, and zip the files
        for(int i=0; i<dirList.length; i++)
        {
            File f = new File(zipDir, dirList[i]);
            if(f.isDirectory())
            {
                    //if the File object is a directory, call this
                    //function again to add its content recursively
                String filePath = f.getPath();
                zipDir(filePath, zos);
                    //loop again
                continue;
            }
            //if we reached here, the File object f was not a directory
            //create a FileInputStream on top of f
            FileInputStream fis = new FileInputStream(f);
            //create a new zip entry
            ZipEntry anEntry = new ZipEntry(f.getPath());
            //place the zip entry in the ZipOutputStream object
            zos.putNextEntry(anEntry);
            //now write the content of the file to the ZipOutputStream
            while((bytesIn = fis.read(readBuffer)) != -1)
            {
                zos.write(readBuffer, 0, bytesIn);
            }
           //close the Stream
           fis.close();
        }
    }
    catch(Exception e)
    {
        //handle exception
    }
}
EN

Stack Overflow用户

回答已采纳

发布于 2012-11-27 15:04:54

我已经设法自己修好了。问题出在下面这一行:

代码语言:javascript
运行
复制
    File f = new File(zipDir, dirList[i]);

它应该是

代码语言:javascript
运行
复制
    File f = new File(dirList[i]);

如果包含参数zipDir,则在归档中将使用目录的绝对路径!

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13566468

复制
相关文章

相似问题

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