我正在尝试制作一个 Android 应用程序,可以打开 docx 文件来读取、编辑和保存它。
我的想法是将存档中的所有 xml 文件提取到临时文件夹中。在这个文件夹中我们可以编辑docx的内容/word/document.xml
。问题是当我压缩这个临时文件夹以创建新的 docx 文件并替换旧文件时,新的 docx 存档内的路径如下/mnt/sdcard/temp/"all files xml go here"
而xml文件应该在第一层。
有人能帮我解决这个问题吗?这是压缩临时目录的方法
注意:我使用的 dir2zip 参数的值是/mnt/sdcard/temp/***.docx
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
}
}
我已经设法自己修好了。问题出在这一行:
File f = new File(zipDir, dirList[i]);
它应该是
File f = new File(dirList[i]);
如果包含参数 zipDir,则归档中将使用目录的绝对路径!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)