【问题标题】:Corrupted docx generated using zipping使用压缩生成的损坏的 docx
【发布时间】:2013-06-08 02:22:17
【问题描述】:

首先让我说我在这里创建了一个帐户,因为我一直在努力解决这个问题,所以就这样吧。

另外,我已经看到了这个问题here. 这些答案都没有帮助,我都试过了。

我需要创建一个word文档,里面有一个简单的表格和数据。我决定创建一个示例文档,在其中获取创建文档所需的 xml。我将解压后的 docx 文件中的所有文件夹移动到我的资产文件夹中。一旦我意识到我无法写入资产文件夹,我编写了一个方法将所有文件和文件夹复制到设备的外部存储位置,然后将我创建的文档写入同一位置。从那里我试图将文件压缩回 .docx 文件。这是事情不工作的地方。

实际的 docx 文件已创建,我可以通过 DDMS 将其移动到我的计算机,但是当我查看它时,Word 说它已损坏。奇怪的是,如果我解压缩它然后在我的计算机上重新压缩它而不进行任何更改,那么它可以完美运行。我使用了一个名为 DiffMerge 的程序(适用于 mac)来将示例解压缩 docx 文件与我创建的解压缩 docx 文件进行比较,它说它们完全相同。所以,我认为这与 Android 中的压缩过程有关

我还尝试在我的计算机上解压缩示例 docx 文件,将所有文件和文件夹移动到我的资产文件夹,包括 document.xml 文件,然后尝试在不添加我自己的 document.xml 文件的情况下将其压缩并使用样品一,也不起作用。我尝试的另一件事是将实际的 docx 文件放在我的资产文件夹中,将其解压缩到我的外部存储上,然后重新压缩它而不做任何事情。这也失败了。

我基本上不知所措。请有人帮我解决这个问题。

这是我的一些代码:

  1. 首先调用moveDocxFoldersFromAssetsToExternalStorage()。
  2. 调用之后,所有文件都已移过。
  3. 然后,我创建document.xml文件,放到它所属的word目录下
  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();
                }
            }
        }
    }
}

【问题讨论】:

  • 在您加载文档时,它们是否对您的错误消息有任何描述?如果没有的话,这个word很有可能打不开这种zip文件。
  • 它说“此文件已损坏,无法打开。”如果我解压缩然后在我的计算机上重新压缩它,Word 可以打开该文件。然后它工作得很好。似乎在 Android 的压缩过程中发生了一些事情,导致它无法解压缩。
  • 唯一能想到的问题就是压缩方式。我使用 jszip-inflate.js 来压缩 docxgen.js 的文件: Demo: javascript-ninja.fr/docxgenjs/examples/simpleTagging.html# Repo github.com/edi9999/docxgenjs 另外,你应该知道你不应该压缩包含文件的文件夹,而是压缩文件本身。跨度>
  • 能否请您发送损坏文件的 zip 文件?
  • 我知道 zip 的根目录下不能有文件夹。不过,我发现这很困难。这是我损坏的File 我不确定如何将您链接到的代码改编为我的 Android 应用程序的 java 代码。我尝试在网上查找是否有任何其他方法可以在 Android 上创建 zip,但找不到任何变体。

标签: java android zip docx


【解决方案1】:

我想我有什么问题。

当我打开你的corrupted File,并在winrar上打开时,我看到文件夹开头有反斜杠,这很不寻常:

当我解压后重新压缩文件时,反斜杠不再存在并且文件在 Word 中打开,所以我认为这应该是问题所在。

我认为这里的代码是错误的:

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

应该是

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

【讨论】:

  • 天啊!你真是个绝世天才!非常感谢。我想这就是我在编程时不使用我的电脑的原因。我在 mac 上使用的所有 zip 提取器从未向我显示过这些信息。我终于像你说的那样让它工作了,但我不得不稍微调整一下代码。生病编辑我上面的帖子以添加代码。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
相关资源
最近更新 更多