如何在android中压缩文件夹以生成docx文件?
我正在尝试制作一个Android应用程序,它可以打开一个docx文件来读取、编辑和保存它 我的想法是将归档文件中的所有xml文件提取到临时文件夹中。在此文件夹中,我们可以在如何在android中压缩文件夹以生成docx文件?,android,docx,zipfile,Android,Docx,Zipfile,我正在尝试制作一个Android应用程序,它可以打开一个docx文件来读取、编辑和保存它 我的想法是将归档文件中的所有xml文件提取到临时文件夹中。在此文件夹中,我们可以在/word/document.xml中编辑docx的内容。问题是,当我压缩这个临时文件夹以生成一个新的docx文件并替换旧文件时,在新的docx归档文件中,路径类似于/mnt/sdcard/temp/“所有文件xml都在这里”,而xml文件应该在第一级 有人能帮我度过这段时间吗?下面是压缩临时目录的方法 注意:我使用的dir2
/word/document.xml
中编辑docx的内容。问题是,当我压缩这个临时文件夹以生成一个新的docx文件并替换旧文件时,在新的docx归档文件中,路径类似于/mnt/sdcard/temp/“所有文件xml都在这里”
,而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
}
}
public void zipDir(字符串dir2zip,ZipOutputStream zos)
{
尝试
{
//基于我们创建的目录创建一个新的文件对象
//必须压缩文件
File zipDir=新文件(dir2zip);
//获取目录内容的列表
字符串[]dirList=zipDir.list();
字节[]读取缓冲区=新字节[2156];
int字节数=0;
//循环浏览目录列表,并压缩文件
对于(int i=0;i我已经设法自己解决了它。问题在于这一行:
File f = new File(zipDir, dirList[i]);
应该是
File f = new File(dirList[i]);
如果包含参数zipDir,则该目录的绝对路径将用于存档!我现在通过以下两个修改,成功地使原始海报的代码在Mac和Windows上运行:
1:为每个目录添加ZipEntry:不要简单地忽略它
2:从ZipEntry名称中删除目录名
注意:zipinfo很有用
这是一个适合我的程序:
import java.io.*;
import java.util.zip.*;
public class zipdoc
{
String savedDir = null;
public void zipDir(String dir2zip, ZipOutputStream zos)
{
try
{
if (savedDir == null)
savedDir = dir2zip;
// 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
System.out.println("Adding dir: " + f);
// create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath().substring(savedDir.length()+1) + "/");
// place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
String filePath = f.getPath();
zipDir(filePath, zos);
// loop again
continue;
}
else if (!f.getName().equals(".DS_Store"))
{
// if we reached here, the File object f was not a directory
// and it's not the MacOSX special .DS_Store
// create a FileInputStream on top of f
System.out.println("Adding file: " + f);
FileInputStream fis = new FileInputStream(f);
// create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath().substring(savedDir.length()+1));
// 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
System.out.println(e);
}
}
public void zipit(String inDir, String outFile)
{
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(outFile)));
zos.setMethod(0);
zos.setMethod(ZipOutputStream.DEFLATED);
zos.setLevel(0);
zipDir(inDir, zos);
zos.finish();
zos.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
public static void main (String args[]) {
zipdoc z1 = new zipdoc();
// Check there are sufficient params if desired
// first param is directory to be 'zipped', second is resulting
// filename (??.docx)
// eg java zipdoc dir1 newDoc.docx
z1.zipit(args[0], args[1]);
System.out.println("Finished creating " + args[1]);
}
}
import java.io.*;
导入java.util.zip.*;
公共类zipdoc
{
字符串savedDir=null;
public void zipDir(字符串dir2zip、ZipOutputStream zos)
{
尝试
{
if(savedDir==null)
savedDir=dir2zip;
//基于我们创建的目录创建一个新的文件对象
//必须压缩文件
File zipDir=新文件(dir2zip);
//获取目录内容的列表
字符串[]dirList=zipDir.list();
字节[]读取缓冲区=新字节[2156];
int字节数=0;
//循环浏览目录列表,并压缩文件
对于(int i=0;ii)如果我在Mac或Windows上运行原始海报代码,在解压缩一个简单的.docx文件并且不更改任何内容后,生成的压缩文件的大小是原始文件的两倍多,Word认为它已损坏。如果我按照OP的建议更改for循环file f=new file中的第一行(zipDir,dirList[i]);
到File f=new File(zipDir,dirList[i]);
它崩溃时出现异常:java.io.FileNotFoundException:[Content\u Types].xml(没有这样的文件或目录)