2012-11-26 112 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]); 
    } 
} 
相關問題