windows 如何使用命令行压缩指定文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1412051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to zip specified folders with Command Line
提问by Tarik
Could you people tell me how to zip specified files into same Zip file. Let me tell how my folders are filled :
你们能告诉我如何将指定的文件压缩到同一个 Zip 文件中吗?让我告诉我的文件夹是如何填充的:
- A task scheduler have backups of my databases and save them into a file daily. It creates 4 database backup daily which means there will be 4 more files daily. So I need to zip newly created backups into same zip file (of course it differs from previous day zip file, the zip file will be created for that day for newly created backup files) and I need to do it automatically. Well I know how to make it automatic. I can use Windows task scheduler to automate things.
- 任务调度程序有我的数据库的备份,并每天将它们保存到一个文件中。它每天创建 4 个数据库备份,这意味着每天会有 4 个以上的文件。所以我需要将新创建的备份压缩到同一个 zip 文件中(当然它与前一天的 zip 文件不同,将为当天新创建的备份文件创建 zip 文件)并且我需要自动执行此操作。嗯,我知道如何让它自动。我可以使用 Windows 任务调度程序来自动化操作。
Thanks.
谢谢。
回答by Eric J.
I use the open source program 7Zipwith a command line like:
我将开源程序7Zip与命令行一起使用,例如:
"C:\Program Files-Zipz.exe" a My.zip MyFileOrFolder
If you're using an NTFS partition, it might also solve your issue to have Windows automatically compress the folder your backup files are in (right click on your folder, select Properties, Advanced, and click Compress contents to secure data).
如果您使用的是 NTFS 分区,让 Windows 自动压缩备份文件所在的文件夹也可能会解决您的问题(右键单击您的文件夹,选择“属性”、“高级”,然后单击“压缩内容以保护数据”)。
回答by Cheeso
You can do it with DotNetZip- it was originally produced as a library for use by programmers but the sample apps built on and shipped with the library have become very useful in their own right.
您可以使用DotNetZip 来实现——它最初是作为一个供程序员使用的库而制作的,但是在该库上构建和附带的示例应用程序本身就变得非常有用。
One of the "samples" is called zipit.exe - a console tool. Specify a non-existent zipfile to create a new one, or specify an existing zipfile to update it.
其中一个“示例”称为 zipit.exe - 一种控制台工具。指定一个不存在的 zipfile 来创建一个新的,或者指定一个现有的 zipfile 来更新它。
zipit.exe ZipArchive.zip c:\data\folder1
You can specify one or more filenames, directories, or a logical expression that describes which files to include. For the expressions:
您可以指定一个或多个文件名、目录或描述要包含哪些文件的逻辑表达式。对于表达式:
name = *.jpg
any .jpg file.mtime > 07/01/2009
any file with a last modified timeafter midnight on 1 July 2009. There is also ctime and atime for created time and accessed time.ctime > 07/01/2009:07:53:00
any file with a created timeafter 7:53am on 1 July 2009.size > 320mb
any file with a size over 320mb. You can use kbor gb, too. Or omit the characters for a size in bytes. And you can use < or = as operations.attr != H
any file that does not have the Hidden attribute set. Other attributes include S=system, R=Readonly, A=Archive. Of course you can test that the attribute is ON as well (using = instead of !=).attr != H and size > 320mb
include the files that satisfy both conditions. You can also use OR as a conjuction. Use parens to group complex expressions.name = *.jpg or name = *.gif
include the files that satisfy one or the other condition.(name = *.jpg) or (name = *.gif)
same as above.(mtime >= 07/01/2009) and (mtime < 07/02/2009)
any file modified on July 1st. From midnight to midnight.(name = *.jpg) AND (mtime >= 07/01/2009) and (mtime < 07/02/2009)
any .jpg file modified on July 1st.(name = *.jpg) and (size >= 100kb) and (mtime >= 07/01/2009) and (mtime < 07/02/2009)
any .jpg file, 100kb or more in size, modified on July 1st.
name = *.jpg
任何 .jpg 文件。mtime > 07/01/2009
最后修改时间在 2009 年 7 月 1 日午夜之后的 任何文件。还有 ctime 和 atime 用于创建时间和访问时间。ctime > 07/01/2009:07:53:00
创建时间在 2009 年 7 月 1 日上午 7:53 之后的 任何文件。size > 320mb
任何大小超过 320mb 的文件。您也可以使用kb或gb。或者省略字节大小的字符。您可以使用 < 或 = 作为操作。attr != H
没有设置隐藏属性的任何文件。其他属性包括 S=system、R=Readonly、A=Archive。当然,您也可以测试该属性是否为 ON(使用 = 而不是 !=)。attr != H and size > 320mb
包含满足这两个条件的文件。您也可以使用 OR 作为连词。使用括号对复杂的表达式进行分组。name = *.jpg or name = *.gif
包含满足一个或另一个条件的文件。(name = *.jpg) or (name = *.gif)
和上面一样。(mtime >= 07/01/2009) and (mtime < 07/02/2009)
7 月 1 日修改的任何文件。从午夜到午夜。(name = *.jpg) AND (mtime >= 07/01/2009) and (mtime < 07/02/2009)
7 月 1 日修改的任何 .jpg 文件。(name = *.jpg) and (size >= 100kb) and (mtime >= 07/01/2009) and (mtime < 07/02/2009)
7 月 1 日修改的任何 .jpg 文件,大小为 100kb 或更大。
With other options on the zipit.exe tool, you can also:
使用 zipit.exe 工具上的其他选项,您还可以:
- specify whether you want to traverse reparse points (like symlinks or junctions, eg "My Music").
- recurse directories or not (default is NOT)
- encrypt the zip with AES or with "regular" zip encryption
- specify a segment size to produce a spanned or segmented zip file.
- produce a self-extracting archive
- 指定是否要遍历重解析点(如符号链接或连接点,例如“我的音乐”)。
- 是否递归目录(默认为 NOT)
- 使用 AES 或“常规” zip 加密对 zip 进行加密
- 指定段大小以生成跨区或分段的 zip 文件。
- 生成自解压存档
Examples:
例子:
zipit.exe Archive.zip -D c:\project1 -r+ "(name = *.xlsx) and (mtime >= 07/01/2009) and (mtime < 07/31/2009)"
zipit.exe Archive.zip -D c:\project1 -r+ "(name = *.xlsx) and (mtime >= 07/01/2009) and (mtime < 07/31/2009)"
- Produce a file, Archive.zip, that contains all the .xslx files in the c:\project1 directory hierarchy, that were modified in July.
- 生成一个文件 Archive.zip,其中包含 c:\project1 目录层次结构中的所有 .xslx 文件,这些文件在 7 月修改过。
zipit.exe Unpack.exe -sfx w -D project2 -r+ "(name = *.pdf) and (size > 100kb)"
zipit.exe Unpack.exe -sfx w -D project2 -r+ "(name = *.pdf) and (size > 100kb)"
- Produce a self-extracting GUI exe, named Unpack.exe, that contains all the .pdf files in the project2 directory hierarchy, that are larger than 100k in size.
- 生成一个名为 Unpack.exe 的自解压 GUI exe,其中包含 project2 目录层次结构中所有大于 100k 的 .pdf 文件。
DotNetZip is free.
DotNetZip 是免费的。
All these features are available in the .NET interface of the library, too. And there's a GUI tool, too.
所有这些功能也可以在库的 .NET 界面中使用。还有一个 GUI 工具。
回答by Adam Liss
I've abandoned WinZip in favor of 7-Zipbecause it's fast, free, efficient, and available for both Windows and Linux.
我已经放弃 WinZip 转而使用7-Zip,因为它快速、免费、高效,并且适用于 Windows 和 Linux。
If you're a WinZip fan, take a look at the WinZip command-line support add-on: http://winzip.com/prodpagecl.htm
如果您是 WinZip 爱好者,请查看 WinZip 命令行支持插件:http: //winzip.com/prodpagecl.htm
You can archive as many files or directories as your Windows command line will allow:
您可以归档 Windows 命令行允许的尽可能多的文件或目录:
wzzip archive.zip dir1 dir2 dir3 ...
回答by Glenn
You can download GNU zip for Windows at http://gnuwin32.sourceforge.net/packages/zip.htm. If you want to got the whole UNIX command line route, zip is available with http://www.cygwin.com/.
您可以在http://gnuwin32.sourceforge.net/packages/zip.htm下载适用于 Windows 的 GNU zip 。如果您想获得整个 UNIX 命令行路由,可以通过http://www.cygwin.com/获得 zip 。
The man page gives this example that seems to address your problem, there are many other command-line options as well:
手册页给出的这个示例似乎可以解决您的问题,还有许多其他命令行选项:
For example, if foo.zip exists and contains foo/file1 and foo/file2, and the directory foo contains the files foo/file1 and foo/file3, then:
例如,如果 foo.zip 存在并包含 foo/file1 和 foo/file2,并且目录 foo 包含文件 foo/file1 和 foo/file3,则:
zip -r foo.zip foo
or more concisely
或更简洁地
zip -r foo foo
will replace foo/file1 in foo.zip and add foo/file3 to foo.zip. After this, foo.zip contains foo/file1, foo/file2, and foo/file3, with foo/file2 unchanged from before.
将替换 foo.zip 中的 foo/file1 并将 foo/file3 添加到 foo.zip。此后,foo.zip 包含 foo/file1、foo/file2 和 foo/file3,其中 foo/file2 与之前相同。
回答by tsilb
Look into an old program called PKZIP. I believe you can also do this with WinZip and 7Zip.
查看一个名为 PKZIP 的旧程序。我相信你也可以用 WinZip 和 7Zip 做到这一点。