Open Office 如何压缩其文件?

发布于 2024-10-16 22:53:00 字数 347 浏览 3 评论 0原文

我正在尝试以编程方式创建 Open Office 电子表格,但由于某种原因,简单地压缩包含所有必要文件的文件夹会使 Open Office 将文件标记为已损坏。

我是怎么到这个地步的?我首先在 Open Office 中创建一个普通电子表格,其中包含一些值。保存后,我将扩展名更改为 .zip 并复制该文件夹。然后,我使用命令行 zip 压缩第二个文件夹,并将文件扩展名更改为 .ods。当尝试打开生成的文件时,我从 Open Office 收到错误消息,指出文件已损坏。

Open Office 是否使用特殊的压缩算法?执行“文件 test.ods”将其显示为压缩的 zip,那么 Open Office 在压缩例程中添加了什么以使其正常工作?

I'm trying to create an Open Office spreadsheet programmatically but for some reason simply compressing a folder with all the necessary files makes Open Office flag the file as corrupted.

How did I get to this? I started by creating a normal spreadsheet in Open Office with some values in it. After saving I change the extension to .zip and make a copy of the folder. I then compress the second folder using command line zip and change the file extension to .ods. When trying to open the resulting file I get an error from Open Office saying the file is corrupt.

Does Open Office use a special compression algorithm? Doing a "file test.ods" shows it as a compressed zip, so what does Open Office add during the compression routine to make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彼岸花ソ最美的依靠 2024-10-23 22:53:00

文档此处。此步骤对我有用:

  1. 将原始文档文件(它是普通的 zip 文件)解压缩到某个目录:

    $ mkdir 文档
    $ cd 文档
    $ 解压缩../document.odt
    
  2. 修改解压后的数据。

  3. 创建一个新的odt:

    $ zip -0 -X ../document2.odt mimetype
    $ zip -r ../document2.odt * -x mimetype
    

Documentation here. This steps worked for me:

  1. Uncompress the original document file (it's a normal zip file) to some directory:

    $ mkdir document
    $ cd document
    $ unzip ../document.odt
    
  2. Modify the uncompressed data.

  3. Create a new odt:

    $ zip -0 -X ../document2.odt mimetype
    $ zip -r ../document2.odt * -x mimetype
    
情定在深秋 2024-10-23 22:53:00

OASIS 第 17 节 OpenOffice 规范 定义了需要如何打包 OpenDocument 包。

第 17.4 节 MIME 类型流内容如下:

如果文档的 MIME 类型
利用现有的包,
然后是包
应该包含一个名为“mimetype”的流。该流应该
包的 zip 的第一个流
文件,它不得被压缩,
并且它不得使用“额外”
其标头中的“field”(请参阅​​[ZIP])..

目的是允许打包文件
通过“魔法”来识别
number' 机制,例如 Unix 的
文件/魔法实用程序。如果是 ZIP 文件
开头包含一个流
未压缩的文件,并且具有
标头中没有额外数据,则
流名称和流内容可以
可以在固定位置找到。更多的
具体来说,人们会发现:

  • 所有 zip 文件的位置 0 处的字符串“PK”
  • 所有此类包文件的位置 30 处的字符串“mimetype”
  • mimetype 本身位于此类包的位置 38。

Section 17 Of the OASIS OpenOffice Specification defines how OpenDocument Packages need to be packaged.

Section 17.4 MIME Type Stream reads like this:

If a MIME type for a document that
makes use of packages is existing,
then the package
SHOULD contain a stream called "mimetype". This stream SHOULD be
first stream of the package's zip
file, it MUST NOT be compressed,
and it MUST NOT use an 'extra
field' in its header (see [ZIP])..

The purpose is to allow packaged files
to be identified through 'magic
number' mechanisms, such as Unix's
file/magic utility. If a ZIP file
contains a stream at the beginning of
the file that is uncompressed, and has
no extra data in the header, then the
stream name and the stream content can
be found at fixed positions. More
specifically, one will find:

  • a string 'PK' at position 0 of all zip files
  • a string 'mimetype' at position 30 of all such package files
  • the mimetype itself at position 38 of such a package.
庆幸我还是我 2024-10-23 22:53:00

这个 anwser 与 @tokland 建议相同,但可以用作命令。例如:./folder2od.sh "/path/to/folder" "file.odt"

#!/usr/bin/env bash

# Convert folder (unzipped OpenDocument file) to OpenDocument file (odt, ods, etc.)
# Usage: ./folder2od.sh "/path/to/folder" "file.odt"

folder=$(cd `dirname "$2"`; pwd)
file=$(basename "$2")
absfile="${folder%%/}/$file"

wd=$(pwd)
cd "$1"

# mimetype file must be the first file, uncompressed
zip -0 -qX - mimetype > "$absfile"
# Other files
zip -DgqrX "$absfile" * -x mimetype

cd "$wd"

您可以在这里找到一些有趣的信息:如何使用 zip 正确创建 ODF 文档 - 孤狼 - Web、游戏和开源开发


编辑:简化在脚本中,似乎只需要 mimetype 作为第一个(未压缩)条目。其他条目的顺序并不重要。

This anwser is the same as @tokland suggestion, but can be used as a command. Ex: ./folder2od.sh "/path/to/folder" "file.odt"

#!/usr/bin/env bash

# Convert folder (unzipped OpenDocument file) to OpenDocument file (odt, ods, etc.)
# Usage: ./folder2od.sh "/path/to/folder" "file.odt"

folder=$(cd `dirname "$2"`; pwd)
file=$(basename "$2")
absfile="${folder%%/}/$file"

wd=$(pwd)
cd "$1"

# mimetype file must be the first file, uncompressed
zip -0 -qX - mimetype > "$absfile"
# Other files
zip -DgqrX "$absfile" * -x mimetype

cd "$wd"

You can found some interesting infos here: How to correctly create ODF documents using zip - Lone Wolves - Web, game, and open source development


Edit: simplify the script, only mimetype seem to be needed to be the first (uncompressed) entry. The order of other entries doesn't matter.

鸩远一方 2024-10-23 22:53:00

即使这是旧的,也是在 2021 年,如果打开的文档是使用 microsoft office、office 365、google docs、libreoffice 或 openoffice 生成的,那么操作打开的文档也像以前一样简单:

  1. 制作文档的副本

  2. 将复制文档的扩展名重命名为 .zip(因为每个打开的文档都是 zip 文件!)

  3. 使用文档名称创建一个文件夹,不带扩展名

  4. 将步骤 2) 中重命名的文档(zip 文件)复制到此文件夹中

  5. 提取此文件夹中的(文档)zip 文件

  6. 删除 zip 文件!

  7. ...根据需要更改 xml 数据和二进制对象

  8. 将所有文件标记为该文件夹中的文件夹并将它们添加到新的 zip 文件中(仅使用标准 zip 压缩!)

  9. 现在您应该在之前在 setp 3 中创建的文件夹中拥有一个新的 zip 文件)

  10. 将此新 zip 文件的扩展名重命名回 o.odt 或 .odp 或您在步骤中重命名的任何原始打开文档类型2)

  11. 尝试在任何能够处理的办公软件中打开这个新的、重命名的打开文档打开文档文件

请记住:

a) 每个打开的文档都是一个(压缩的)zip 文件

b) 该 zip 文件包含以下 xml 文件表示该文档的结构和文本内容,它还包含具有二进制数据(对象)的子文件夹,例如媒体数据(图像、音频或视频数据以及 ole 对象),其中一些可能会显示为 xml 文件中的 base64 编码。

c) 您可以将每个打开的文档的内容提取到一个新文件夹中

d) 切勿压缩放置所有数据的文件夹,以创建新的 zip 文件/打开的文档文件。 仅压缩此文件夹的内容,以创建有效的打开文档,并将如此创建的 zip 文件重命名为其原始源文件使用的打开文档扩展名!

来源:https://en.wikipedia.org/wiki/OpenDocument_technical_specification

您使用的工具可用于操作打开的文档文件:

a) https://7-zip.de/ download.html(提取和压缩)

b) https://notepad-plus- plus.org/downloads/(编辑 XML 内容)

c) https://www.plus.org/downloads/( 编辑 XML 内容) bulkrenameutility.co.uk/(批量重命名文件和文件夹,如果你不知道windows、linux下的命令...请参阅:https://unix.stackexchange.com/questions/181141/rename-multiple-files-with-mv-to-change-the -扩展名)

Even if this is old, also in 2021, to manipulate open documents works as easy as before, if it was generated with microsoft office, office 365, google docs, libreoffice or openoffice:

  1. make a copy of you document

  2. rename extension of the copied document to .zip (because every open document is a zip file!)

  3. create a folder with the document name, without extension

  4. copy the renamed document (zip file) from step 2) into this folder

  5. extract the (document) zip file within this folder

  6. delete the zip file!

  7. ... change xml data and binary objects as you like

  8. mark all files an folders within this folder and add them to a new zip file (only use standard zip compression!)

  9. now you should have a new zip file within the folder you created before in setp 3)

  10. rename the extension of this new zip file back to o.odt or .odp or whatever the original open document type was, you renamed in step 2)

  11. try to open this new, renamed open document in any office software able to handle open document files

Please remember:

a) every open document is a (compressed) zip file

b) the zip file contains xml files which represent the structure and text-content of this document and it also contains supfolders with binary datas (objects), like media data (images, audio or video data, and ole objects), some of them may appear as base64 coded within an xml file.

c) you can extract the content of each open document into a new folder

d) never compress the folder where you put all your data, to create a new zip file/open document file. ONLY compress the content of this folder, to create a valid open document and rename the so created zip file to the open document extension his original source file used!

Sources: https://en.wikipedia.org/wiki/OpenDocument_technical_specification

Tools you can use to manipulate open document files:

a) https://7-zip.de/download.html (to extract and compress)

b) https://notepad-plus-plus.org/downloads/ (to edit the XML content)

c) https://www.bulkrenameutility.co.uk/ (to bulk rename files and folders if you do not know the command under windows, linux ...see: https://unix.stackexchange.com/questions/181141/rename-multiple-files-with-mv-to-change-the-extension)

叫嚣ゝ 2024-10-23 22:53:00

shell 脚本也对我有用:) 在解压缩 odt 文件后,我在压缩备份时遇到了问题。猜猜清单部分是缺失的。

然而,上面的 shell 脚本不处理内联图片/图形,所以我做了一些对我有用的小调整(此外,该脚本有一个错误,END_HEREDOC 不在专用线上):

#!/bin/sh

# Convert folder (unzipped OpenDocument file) to OpenDocument file (odt, ods, etc.)
# Usage: ./folder2od.sh "path/to/folder" "file.odt"

cmdfolder=$(cd `dirname "$0"`; pwd -P)
folder=$(cd `dirname "$2"`; pwd -P)
file=$(basename "$2")
absfile="$folder/$file"

cd "$1"
zip -0 -X "$file" "mimetype"

list=$(cat <<'END_HEREDOC'
meta.xml
settings.xml
content.xml
Pictures/
Thumbnails/
Configurations2/
styles.xml
manifest.rdf
META-INF/manifest.xml
END_HEREDOC
)

for f in $list
do
    zip -r "$absfile" "$f"
done

cd "$cmdfolder"

The shell script worked for me, too :) I had problems zipping back up, after unzipping an odt file. Guess the manifest part was what's missing.

The shell script above did not handle inline pictures/graphics, however, so I made some small adjustments which worked for me (also, the script had a bug in that END_HEREDOC was not on a dedicated line):

#!/bin/sh

# Convert folder (unzipped OpenDocument file) to OpenDocument file (odt, ods, etc.)
# Usage: ./folder2od.sh "path/to/folder" "file.odt"

cmdfolder=$(cd `dirname "$0"`; pwd -P)
folder=$(cd `dirname "$2"`; pwd -P)
file=$(basename "$2")
absfile="$folder/$file"

cd "$1"
zip -0 -X "$file" "mimetype"

list=$(cat <<'END_HEREDOC'
meta.xml
settings.xml
content.xml
Pictures/
Thumbnails/
Configurations2/
styles.xml
manifest.rdf
META-INF/manifest.xml
END_HEREDOC
)

for f in $list
do
    zip -r "$absfile" "$f"
done

cd "$cmdfolder"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文