2012-11-26 80 views
2

我正在尝试制作一个Android应用程序,可以打开一个docx文件来阅读,编辑和保存它。如何在android中压缩文件夹以制作docx文件?

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

有人能帮助我解决这个问题吗?这里是压缩temp目录

注意的方法:我用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 
    } 
} 

回答

0

我已成功由我自己来解决它。问题是,在这条线:

File f = new File(zipDir, dirList[i]); 

应该

File f = new File(dirList[i]); 

如果参数zipDir包括,对目录的绝对路径将在存档使用!

+0

如果我在Mac或Windows上运行原始海报的代码,解压缩一个简单的.docx文件并且不更改任何内容后,生成的压缩文件是原始文件大小的两倍,Word认为它已损坏。 如果我改变,如由OP建议的,内侧的第一行中的for循环 '文件f =新的文件(zipDir,dirList [I]);' 到 '文件f =新的文件(zipDir, dirList [i]);' 崩溃时发生异常: 'java.io.FileNotFoundException:[Content_Types] .xml(No such file or directory)'' – ghr

0

我现在已经设法让楼主的代码在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]); 
    } 
} 
相关问题