How to zip specified folders with Command Line

17,953

Solution 1

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.

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 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 time after 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 time after 7:53am on 1 July 2009.

  • size > 320mb
    any file with a size over 320mb. You can use kb or 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.

With other options on the zipit.exe tool, you can also:

  • 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

Examples:

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.

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.

DotNetZip is free.

All these features are available in the .NET interface of the library, too. And there's a GUI tool, too.

Solution 2

I use the open source program 7Zip with a command line like:

"C:\Program Files\7-Zip\7z.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).

Solution 3

I've abandoned WinZip in favor of 7-Zip because it's fast, free, efficient, and available for both Windows and Linux.

If you're a WinZip fan, take a look at the WinZip command-line support add-on: http://winzip.com/prodpagecl.htm

You can archive as many files or directories as your Windows command line will allow:

wzzip archive.zip dir1 dir2 dir3 ...

Solution 4

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/.

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:

      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.

Solution 5

Look into an old program called PKZIP. I believe you can also do this with WinZip and 7Zip.

Share:
17,953
Tarik
Author by

Tarik

I am a software engineer with interests in different technologies.

Updated on June 04, 2022

Comments

  • Tarik
    Tarik over 1 year

    Could you people tell me how to zip specified files into same Zip file. Let me tell how my folders are filled :

    • 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.

    Thanks.