2013-06-08 2 views
2

방금 ​​벽을 머리에 맞기 때문에 여기에 계정을 만들었다 고 말하면서 시작해 봅니다.지오 코딩을 사용하여 손상된 docx 생성

또한이 질문을 이미 보았습니다. here. 두 답변 중 하나도 도움이되지 않았고 둘 다 시도했습니다.

간단한 테이블과 데이터로 된 워드 문서를 만들어야합니다. 나는 문서를 만들 필요가있는 XML을 얻기위한 예제 문서를 만들기로 결정했다. 모든 폴더를 압축 해제 된 docx 파일에서 내 자산 폴더로 이동했습니다. 자산 폴더에 쓸 수 없다는 것을 알게되면 모든 파일과 폴더를 장치의 외부 저장소 위치로 복사 한 다음 내가 만든 문서를 같은 위치에 쓸 수있는 방법을 썼습니다. 거기서부터 .docx 파일까지 파일을 압축하려고합니다. 이것은 일들이 작동하지 않는 곳입니다.

실제 docx 파일이 만들어지고 DDMS를 통해 내 컴퓨터로 옮길 수 있지만 볼 때 Word가 손상되었다고 말합니다. Heres는 이상하지만, 압축을 풀고 내 컴퓨터에서 아무 것도 변경하지 않고 다시 압축하면 완벽하게 작동합니다. DiffMerge라는 프로그램을 사용하여 샘플 압축을 푼 docx 파일을 내가 작성한 압축 해제 된 docx 파일과 비교했습니다.이 압축 파일은 정확히 동일합니다. 그래서, 나는 그것이 안드로이드의 압축 과정과 관련이 있다고 생각한다..

또한 내 컴퓨터에서 샘플 docx 파일의 압축을 풀어 모든 파일과 폴더를 document.xml 파일을 비롯한 내 자산 폴더로 이동 한 다음 내 자신의 document.xml 파일을 추가하지 않고 압축하려고했습니다. 샘플 1을 사용하면 그 중 하나가 작동하지 않습니다. 내가 시도한 또 다른 일은 내 자산 저장소에 실제 docx 파일을 배치하고, 내 외부 저장소에 압축을 풀고 아무것도하지 않고 압축을 푸는 것입니다. 이것은 또한 실패합니다.

나는 기본적으로 잃어 버렸습니다. 누군가 제게 이것을 이해하도록 도와주세요.

  1. moveDocxFoldersFromAssetsToExternalStorage()를 먼저 호출됩니다

    여기 내 코드의 일부이다.

  2. 그 후 모든 파일이 이동되었습니다.
  3. 그런 다음 document.xml 파일을 만들어 단어 디렉토리에 넣습니다.
  4. 모든 것이 있어야하고 이제는 zip 파일을 만들려고합니다.

.

private boolean moveDocxFoldersFromAssetsToExternalStorage(){ 
    File rootDir = new File(this.externalPath); 
    rootDir.mkdir(); 

    copy(""); 

    // This is to get around a glitch in Android which doesnt list files or folders 
    // with an underscore at the beginning of the name in the assets folder. 
    // This renames them once they are saved to the device. 
    // We need it to show up in the list in order to move them. 

    File relsDir = new File(this.externalPath + "/word/rels"); 
    File renameDir = new File(this.externalPath + "/word/_rels"); 
    relsDir.renameTo(renameDir); 

    relsDir = new File(this.externalPath + "/rels"); 
    renameDir = new File(this.externalPath + "/_rels"); 
    relsDir.renameTo(renameDir); 

    // This is to get around a glitch in Android which doesnt list hidden files. 
    // We need it to show up in the list in order to move it. 

    relsDir = new File(this.externalPath + "/_rels/rels.rename"); 
    renameDir = new File(this.externalPath + "/_rels/.rels"); 
    relsDir.renameTo(renameDir); 

    return true; 
} 

private void copy(String outFileRelativePath){ 
    String files[] = null; 
    try { 
     files = this.mAssetManager.list(ASSETS_RELATIVE_PATH + outFileRelativePath); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    String assetFilePath = null; 
    for(String fileName : files){ 
     if(!fileName.contains(".")){ 
      String outFile = outFileRelativePath + java.io.File.separator + fileName; 
      copy(outFile); 
     } else { 
      File createFile = new File(this.externalPath + java.io.File.separator + outFileRelativePath); 
      createFile.mkdir(); 
      File file = new File(createFile, fileName); 

      assetFilePath = 
       ASSETS_RELATIVE_PATH + outFileRelativePath + java.io.File.separator + fileName; 

      InputStream in = null; 
      OutputStream out = null; 
      try { 
       in = this.mAssetManager.open(assetFilePath); 
       out = new FileOutputStream(file); 
       copyFile(in, out); 
       in.close(); 
       out.flush(); 
       out.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1){ 
     out.write(buffer, 0, read); 
    } 
} 

private void zipFolder(String srcFolder, String destZipFile) throws Exception{ 
    FileOutputStream fileWriter = new FileOutputStream(destZipFile); 
    ZipOutputStream zip = new ZipOutputStream(fileWriter); 
    zip.setMethod(Deflater.DEFLATED); 
    zip.setLevel(ZipOutputStream.STORED); 

    addFolderToZip(this.externalPath, "", zip); 

    zip.finish(); 
    zip.close(); 
} 


private void addFolderToZip(String externalPath, String folder, ZipOutputStream zip){ 
    File file = new File(externalPath); 
    String files[] = file.list(); 

    for(String fileName : files){ 
     try { 
      File currentFile = new File(externalPath, fileName); 
      if(currentFile.isDirectory()){ 
       String outFile = externalPath + java.io.File.separator + fileName;   
       addFolderToZip(outFile, folder + java.io.File.separator + fileName, zip); 
      } else { 
       byte[] buffer = new byte[8000]; 
       int len; 
       FileInputStream in = new FileInputStream(currentFile); 
       zip.putNextEntry(new ZipEntry(folder + java.io.File.separator + fileName)); 
       while((len = in.read(buffer)) > 0){ 
        zip.write(buffer, 0, len); 
       } 
       zip.closeEntry(); 
       in.close(); 
      } 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
    } 
} 

편집 여기

가 올바르게 edi9999 @ 아래의 말에 따라 작업을 진행하기 내가 위해 쓴 코드입니다. 나는 확장하고 추가하고 아마 약간을 청소하려고하는 별도의 클래스를 만들었지 만, 이것은 작동하는 코드입니다. 디렉토리의 모든 파일을 zip 파일에 추가하고 재귀 적으로 자신을 호출하여 모든 하위 파일과 폴더를 추가합니다.

private class Zip { 
    private ZipOutputStream mZipOutputStream; 
    private String pathToZipDestination; 
    private String pathToFilesToZip; 

    public Zip(String pathToZipDestination, String pathToFilesToZip) { 
     this.pathToZipDestination = pathToZipDestination; 
     this.pathToFilesToZip = pathToFilesToZip; 
    } 

    public void zipFiles() throws Exception{ 
     FileOutputStream fileWriter = new FileOutputStream(pathToZipDestination); 
     this.mZipOutputStream = new ZipOutputStream(fileWriter); 
     this.mZipOutputStream.setMethod(Deflater.DEFLATED); 
     this.mZipOutputStream.setLevel(8); 

     AddFilesToZip(""); 

     this.mZipOutputStream.finish(); 
     this.mZipOutputStream.close(); 
    } 

    private void AddFilesToZip(String folder){ 
     File mFile = new File(pathToFilesToZip + java.io.File.separator + folder); 
     String mFiles[] = mFile.list(); 

     for(String fileName : mFiles){ 
      File currentFile; 
      if(folder != "") 
       currentFile = new File(pathToFilesToZip, folder + java.io.File.separator + fileName); 
      else 
       currentFile = new File(pathToFilesToZip, fileName); 
      if(currentFile.isDirectory()){ 
       if(folder != "") 
        AddFilesToZip(folder + java.io.File.separator + currentFile.getName()); 
       else 
        AddFilesToZip(currentFile.getName()); 
      } else { 
       try{ 
        byte[] buffer = new byte[8000]; 
        int len; 
        FileInputStream in = new FileInputStream(currentFile); 
        if(folder != ""){ 
         mZipOutputStream.putNextEntry(new ZipEntry(folder + java.io.File.separator + fileName)); 
        } else { 
         mZipOutputStream.putNextEntry(new ZipEntry(fileName)); 
        } 
        while((len = in.read(buffer)) > 0){ 
         mZipOutputStream.write(buffer, 0, len); 
        } 
        mZipOutputStream.closeEntry(); 
        in.close(); 
       } catch (IOException e){ 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 
+0

해야합니까? 그렇지 않은 경우,이 종류의 zip 파일을 열 수 없다는 가능성이 큽니다. – edi9999

+0

"이 파일이 손상되어 열 수 없습니다." Word를 압축을 풀고 내 컴퓨터에서 다시 압축하면 파일을 열 수 있습니다. 그렇다면 잘 작동합니다. 그것은 안드로이드의 지퍼 링 과정에서 진행되는 무언가처럼 보입니다. – Sabastino

+0

제가 생각할 수있는 유일한 문제는 압축 방법입니다. docxgen.js 파일을 압축하는 데 jszip-inflate.js를 사용합니다. 데모 : http://javascript-ninja.fr/docxgenjs/examples/simpleTagging.html# Repo https://github.com/edi9999/docxgenjs 또한 파일이 들어있는 폴더는 압축하지 말고 파일 자체는 압축하지 않아야합니다. – edi9999

답변

3

내가 잘못 생각한 것 같습니다.나는 당신의 corrupted File를 개설하고, WinRAR과에 오픈

때, 나는 특이한 폴더의 시작 부분에 antislashes을 보았다

Content of the corrupted Zip

나는 antislashes을 압축 해제 후 파일을 rezip 때 거기에 더 이상 없으며 파일이 Word에서 열리기 때문에 문제가되어야한다고 생각합니다.

Content of the corrected Zip

나는 코드가 여기에 잘못된 생각 : 그들은 당신이 당신의 문서를로드하여 오류 메시지에 대한 어떤 설명

String outFile = externalPath + java.io.File.separator + fileName; 

if (externalPath=="") 
    String outFile = externalPath + fileName; 
else 
    String outFile = externalPath + java.io.File.separator + fileName; 
+0

오 마이 갓! 너 천재적 인 천재 야! 정말 고맙습니다. 내 프로그래밍을 할 때 내 PC를 사용하지 않는 것 같아. Mac에서 사용 된 모든 zip 추출기는 그 정보를 절대로 보여주지 못했습니다. 나는 마침내 당신이 말한 것처럼 일함으로써 그것을 얻었지만 약간의 코드를 수정해야했습니다. 아픈 코드를 추가하려면 위의 내 게시물을 편집하십시오. 다시 한 번 감사드립니다! – Sabastino

관련 문제