简体   繁体   English

如何在 linux 上使用 zip 生成新存档,而不仅仅是刷新存档中的文件并将文件添加到其中?

[英]How to use zip to generate a new archive but not just refresh files in the archive and add files into it, on linux?

Here, (on linux)在这里,(在 Linux 上)

there is an existing archive named A.zip, which include File1 and File2:存在名为 A.zip 的现有存档,其中包括 File1 和 File2:

A.zip:
  File1
  File2

and I run this command: zip A.zip File1 File3 , then the archive A.zip becomes like:我运行这个命令: zip A.zip File1 File3 ,然后存档 A.zip 变成这样:

A.zip:
  File1
  File2
  File3

however, what I really want to get is a brand new archive A.zip: like:然而,我真正想要的是一个全新的存档 A.zip:比如:

A.zip:
  File1
  File3

I know it can be done by run rm A.zip and then run zip A.zip File1 File3 , but it is not elegant and if I write these commands into a shell script so A.zip may not exist while the action to remove a non-existent file is not elegant as well.我知道可以通过运行rm A.zip然后运行zip A.zip File1 File3来完成,但这并不优雅,如果我将这些命令写入 shell 脚本,那么 A.zip 可能不存在,而删除操作不存在的文件也不优雅。

Is there any options for me to get this done?我有什么选择可以完成这项工作吗?

Use these option to works:使用这些选项来工作:

zip -FSr A.zip File1 File3

OPTIONS选项

-FS -FS

          Synchronize the contents of an archive with the files on the OS.  Normally when an archive is updated, new files are added and changed files are updated but files that no longer exist on  the
          OS  are  not deleted from the archive.  This option enables a new mode that checks entries in the archive against the file system.  If the file time and file size of the entry matches that of
          the OS file, the entry is copied from the old archive instead of being read from the file system and compressed.  If the OS file has changed, the entry is read and compressed  as  usual.   If
          the  entry in the archive does not match a file on the OS, the entry is deleted.  Enabling this option should create archives that are the same as new archives, but since existing entries are
          copied instead of compressed, updating an existing archive with -FS can be much faster than creating a new archive.  Also consider using -u for updating an archive.

          For this option to work, the archive should be updated from the same directory it was created in so the relative paths match.  If few files are being copied from the old archive,  it  may  be
          faster to create a new archive instead.

          Note  that  the timezone environment variable TZ should be set according to the local timezone in order for this option to work correctly.  A change in timezone since the original archive was
          created could result in no times matching and recompression of all files.

          This option deletes files from the archive.  If you need to preserve the original archive, make a copy of the archive first or use the --out option to output the  updated  archive  to  a  new
          file.  Even though it may be slower, creating a new archive with a new archive name is safer, avoids mismatches between archive and OS paths, and is preferred.

-r -r

          Travel the directory structure recursively; for example:

                 zip -r foo.zip foo

          or more concisely

                 zip -r foo foo

          In this case, all the files and directories in foo are saved in a zip archive named foo.zip, including files with names starting with ".", since the recursion does not use the  shell's  file-
          name  substitution  mechanism.  If you wish to include only a specific subset of the files in directory foo and its subdirectories, use the -i option to specify the pattern of files to be in‐
          cluded.  You should not use -r with the name ".*", since that matches ".."  which will attempt to zip up the parent directory (probably not what was intended).

          Multiple source directories are allowed as in

                 zip -r foo foo1 foo2

          which first zips up foo1 and then foo2, going down each directory.

          Note that while wildcards to -r are typically resolved while recursing down directories in the file system, any -R, -x, and -i wildcards are applied to internal archive pathnames once the di‐
          rectories  are  scanned.  To have wildcards apply to files in subdirectories when recursing on Unix and similar systems where the shell does wildcard substitution, either escape all wildcards
          or put all arguments with wildcards in quotes.  This lets zip see the wildcards and match files in subdirectories using them as it recurses.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM