大网数据广告活动
服务器运维问答

How to zip files with a size limit?

· 52CE 我爱测

I have a script that zip files from a folder. I want to make sure that the zipped file is not more than 10 MB. If the size is more than 10MB, it should create another ZIP file. Is there any command (or other method )that can be u…

问题原文

原帖链接 · Vishnu · CC BY-SA 3.0

I have a script that zip files from a folder. I want to make sure that the zipped file is not more than 10 MB. If the size is more than 10MB, it should create another ZIP file.

Is there any command (or other method )that can be used for this?

已采纳解答

原帖链接 · ricmarques · CC BY-SA 3.0

You can use the "split archive" functionality of "zip" itself using the "--split-size" option.

From "zip" manpage ("man zip"):

(...)

One use of split archives is storing a large archive on multiple remov‐
able media. For a split archive with 20 split files the files are typ‐
ically named (replace ARCHIVE with the name of your archive) AR‐
CHIVE.z01, ARCHIVE.z02, ..., ARCHIVE.z19, ARCHIVE.zip. Note that the
last file is the .zip file.

(...)

-s splitsize
--split-size splitsize

Split size is a number optionally followed by a multiplier.
Currently the number must be an integer. The multiplier can
currently be one of k (kilobytes), m (megabytes), g (gigabytes),
or t (terabytes). As 64k is the minimum split size, numbers
without multipliers default to megabytes. For example, to cre‐
ate a split archive called foo with the contents of the bar
directory with splits of 670 MB that might be useful for burning
on CDs, the command:

                zip -s 670m -r foo bar

could be used.

So, to create a split zip archive, you could do the following (the "-r" is the "recursive" switch to include subdirectories of the directory):

$ zip -r -s 10m archive.zip directory/

To unzip the file, the "zip" manpage explains that you should use the "-s 0`" switch:

(...)

 zip -s 0 split.zip --out unsplit.zip

will convert a split archive to a single-file archive.

(...)

So, you first "unsplit" the ZIP file using the "-s 0" switch:

$ zip -s 0 archive.zip --out unsplit.zip

... and then you unzip the unsplit file:

$ unzip unsplit.zip

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。