7-zip 命令行

发布于 2024-08-04 01:45:39 字数 275 浏览 3 评论 0原文

我正在创建一个备份实用程序,用于压缩文本文件中列出的所有文件。我使用 MS ACCESS 作为前端,并使用 7-Zip 进行压缩。这些是我正在使用的命令:

7zG.exe a c:\Backup\backup.zip
@c:\temp\tmpFileList.txt

所有文件都被压缩,没有它们的路径。有没有办法在 zip 文件中包含每个文件的路径?然后,当解压 backup.zip 时,文件将恢复到其原始文件夹结构。

谢谢

I'm creating a backup utility that zips all the files listed in a text file. I am using MS ACCESS as my front-end and 7-Zip for compression. These are the commands I am using:

7zG.exe a c:\Backup\backup.zip
@c:\temp\tmpFileList.txt

All the files are compressed without their path. Is there a way to include the path for each file in the zip file? Then when backup.zip is extracted the files would be restored to their original folder structure.

Thanks

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

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

发布评论

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

评论(8

青巷忧颜 2024-08-11 01:45:39

这个 7-zip 论坛帖子中,许多人表示为了表达对这一功能的渴望,7-zip 的开发人员 Igor 指出了常见问题解答,标题为“我怎样才能在存档中存储文件的完整路径?”以获得类似的结果。

简而言之:

  • 按卷分隔文件(一个列表用于 C:\ 上的文件,一个列表用于 D:\ 等),
  • 然后对于每个卷的文件列表,
    1. chdir 到相应卷的根目录(例如,cd /d C:\
    2. 创建一个文件列表,其中包含相对于卷根目录的路径(例如,C:\Foo\Bar 变为 Foo\Bar
    3. 像以前一样使用这个新文件列表执行7z a archive.7z @filelist
    4. 使用完整路径提取时,请确保先 chdir 到相应卷的根目录

In this 7-zip forum thread, in which many people express their desire for this feature, 7-zip's developer Igor points to the FAQ question titled "How can I store full path of file in archive?" to achieve a similar outcome.

In short:

  • separate files by volume (one list for files on C:\, one for D:\, etc)
  • then for each volume's list of files,
    1. chdir to the root directory of the appropriate volume (eg, cd /d C:\)
    2. create a file listing with paths relative to the volume's root directory (eg, C:\Foo\Bar becomes Foo\Bar)
    3. perform 7z a archive.7z @filelist as before with this new file list
    4. when extracting with full paths, make sure to chdir to the appropriate volume's root directory first
红墙和绿瓦 2024-08-11 01:45:39

7-Zip 的命令行程序是 7z 或 7za。这是关于可用选项的有用的帖子。 -r(递归)选项存储路径。

The command-line program for 7-Zip is 7z or 7za. Here's a helpful post on the options available. The -r (recurse) option stores paths.

诗笺 2024-08-11 01:45:39

我没有研究过这一点,但我想说的是,他们在便携式设备中放弃了命令行支持。人们不在便携式应用程序中执行太多命令行操作的原因是操作系统(在您的情况下是 Windows)要求将可执行文件添加到 %path% 包含列表中。

如果不满足该要求,则使用命令行实用程序是相当乏味的。

7z -a .

将是

d:\portable\z7\z7 -ac:\to\archive\folder*.*

为所有内容键入该内容就是为什么 GUI 对于便携式应用程序之类的东西有意义(该应用程序)可以记住它自己的位置并为您处理这些内容,如果您无法运行它,您知道它没有附加。

如果您确实希望便携式应用程序包含该应用程序,那么您始终可以安装完整版本并将所需的 7z.exe 取出并将其放入便携式文件夹中,确保它与所需的 dll 一起。

确保外壳已连接后,您必须在敲击外壳时设置路径。

http://www.redfernplace.com/software-projects/patheditor/ --如果您有大量 20+ 的路径信息难以阅读,那么一个好的路径编辑器(向下)很有用。

http://www.softpedia.com/get/System/System -Miscellaneous/Path-Editor.shtml -- 路径编辑器的alternet源代码

不建议修改临时“便携式”驱动器的系统路径,尽管手动执行此操作:

set path=%path%;"d:\便携式\z7\";

当您运行 dos cmd.exe 或 http://sourceforge.net/p/conemu/home/ Home/

其他答案可以更好地解决其他问题,我不会尝试..

http://www.codejacked.com/zip-up-files-from-the-command-line/ -- z7 和 z7a 命令行使用的良好参考。

PS:对死灵感到抱歉,但我认为它需要一个更直接的答案来解释原因(即使这只是推测)。

I've not looked into this but shooting from the hip I'd say that they dropped command line support in the portable. The reason people don't do much command line stuff in portable applications is that the OS (windows in your case) requires that executables be added to the %path% inclusion list.

If that requirement is not met using command line utilities is rather tedious.

7z -a .

would be

d:\portable\z7\z7 -a c:\to\archive\folder*.*

Typing that out for everything is why GUI's make sense with things like portable apps it (the app) can remember it's own location and handle that stuff for you and if you can't run it you know it's not attached.

If you really want the portable app to contain that though you can always install the full version and pull the required 7z.exe out and put it into the portable folder making sure it's in with the required dll's.

You'll have to set your path when you hit the shell after making sure it's attached.

http://www.redfernplace.com/software-projects/patheditor/ -- a good path editor (down) usefull if you have lots of path information 20+ get's hard to read.

http://www.softpedia.com/get/System/System-Miscellaneous/Path-Editor.shtml -- alternet source for path editor

It's not advisable to modify your system path for temproary "portable" drives though manualy do that by:

set path=%path%;"d:\portable\z7\";

when you run dos cmd.exe or http://sourceforge.net/p/conemu/home/Home/

The other answers address other problems better I'm not going to try..

http://www.codejacked.com/zip-up-files-from-the-command-line/ -- good reference for command line usage of z7 and z7a.

PS: sorry for the necro but I figured it needed a more direct answer to why (even if it's just speculative).

心安伴我暖 2024-08-11 01:45:39

试试这个。它对我有用。
7z.exe ad:\newFileName.7z "d:\ExistingFile.txt"

打开 cmd,如果您已经安装了 7zip 应用程序,请尝试此操作。
在命令提示符下,它将类似于 c:\programs and files\7zip\7z.exe ad:\newFileName.7z "d:\ExistingFile.txt"

try this one. it worked for me.
7z.exe a d:\newFileName.7z "d:\ExistingFile.txt"

open cmd and if you have installed 7zip app then try this.
on command prompt it will like c:\programs and files\7zip\7z.exe a d:\newFileName.7z "d:\ExistingFile.txt"

心清如水 2024-08-11 01:45:39

从 7-zip 版本 9.25 alpha 开始,有一个新的 -spf 开关,可用于存储完整的文件路径,包括存档的驱动器号。

7zG.exe a -spf c:\BAckup\backup.zip @c:\temp\tmpFileList.txt

现在应该工作得很好。

Since 7-zip version 9.25 alpha there is a new -spf switch that can be used to store the full file paths including drive letter to the archive.

7zG.exe a -spf c:\BAckup\backup.zip @c:\temp\tmpFileList.txt

should be working just fine now.

潜移默化 2024-08-11 01:45:39

7-Zip 需要列表文件中的相对路径,否则它将仅存储文件名,从而导致重复文件名错误。

假设您的列表包含完整路径名:

  • 编辑列表文件以删除驱动器前缀 C:\
  • 确保在运行 7Z 时位于驱动器的根目录中以使用上述列表文件。
  • 然后它会存储路径并且不会抱怨重复的名称。它需要列表文件中的相对路径。

如果您的列表文件具有相对于另一个文件夹的路径,则您应该从该文件夹运行 7Z。

更新:我从上面的另一篇文章中注意到,新的 7-Zip 有一个 -spf 选项,不需要上述步骤。尚未测试,但我的步骤适用于没有此选项的早期版本。

7-Zip wants relative paths in the list file otherwise it will store only the filenames, causing duplicate file name error.

Assuming that your list contains full path names:

  • Edit the list file to remove drive prefix, C:\
  • Make sure you are in the root of the drive when you run 7Z to use the above list file.
  • Then it will store the paths and won't complain of the duplicate name. It wants relative paths in the list file.

If your list file has paths relative to another folder, you should be running 7Z from that folder.

Update: I noticed from another post above that the new 7-Zip has an -spf option that doesn't require the above steps. Not tested it yet but my steps are for earlier versions that do not have this option.

一个人的旅程 2024-08-11 01:45:39

我创建了以下批处理文件,以利用 7-Zip 将每个子文件夹压缩到其自己的 zip 文件中,并保留文件结构。由于这有效地演示了命令行,我认为它可能会帮助其他人将其发布在这里。

@ECHO OFF
REM Cycle Through all of the directories in the current folder
for /D %%d in (*) do (
    REM Change to the directory
    pushd %%d
    REM Make the destination directory.
    mkdir "Z:\%%d"

    REM Cycle Through all Subdirectories
    for /D %%b in (*) do (
        REM Zip up all subdirectores, and put it in the destination.
        7z -r a "Z:\%%d\%%b.zip" %%b
    )
    popd
)

I created the following batch file to utilize 7-Zip to zip up every subfolder into its own zip file, preserving the file structure. Since this demonstrates the commandline effectively, I thought it might help someone else to post it here.

@ECHO OFF
REM Cycle Through all of the directories in the current folder
for /D %%d in (*) do (
    REM Change to the directory
    pushd %%d
    REM Make the destination directory.
    mkdir "Z:\%%d"

    REM Cycle Through all Subdirectories
    for /D %%b in (*) do (
        REM Zip up all subdirectores, and put it in the destination.
        7z -r a "Z:\%%d\%%b.zip" %%b
    )
    popd
)
百合的盛世恋 2024-08-11 01:45:39

使用选项 x 代替选项 a,这将创建目录,但仅用于提取,而不是压缩。

Instead of the option a use option x, this will create the directories but only for extraction, not compression.

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