{"id":8265,"date":"2019-01-17T10:22:04","date_gmt":"2019-01-17T10:22:04","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=8265"},"modified":"2019-03-08T21:57:25","modified_gmt":"2019-03-08T21:57:25","slug":"zipping-files-on-linux-the-many-variations-and-how-to-use-them","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/17\/zipping-files-on-linux-the-many-variations-and-how-to-use-them\/","title":{"rendered":"Zipping files on Linux: the many variations and how to use them"},"content":{"rendered":"<h3>There are quite a few interesting things that you can do with &#8220;zip&#8221; commands other than compress and uncompress files. Here are some other zip options and how they can help.<\/h3>\n<div class=\"lede-container\">\n<figure class=\"hero-img\"><img decoding=\"async\" src=\"https:\/\/images.idgesg.net\/images\/article\/2019\/01\/zipper-100785364-large.jpg\" alt=\"how to zip files on Linux\" \/><\/figure>\n<figure class=\"hero-img\"><span style=\"font-size: 1rem;\">Some of us have been zipping files on Unix and Linux systems for many decades \u2014 to save some disk space and package files together for archiving. Even so, there are some interesting variations on zipping that not all of us have tried. So, in this post, we\u2019re going to look at standard zipping and unzipping as well as some other interesting zipping options.<\/span><\/figure>\n<h2>The basic zip command<\/h2>\n<p>First, let\u2019s look at the basic\u00a0<strong>zip<\/strong>\u00a0command. It uses what is essentially the same compression algorithm as\u00a0<strong>gzip<\/strong>, but there are a couple important differences. For one thing, the gzip command is used only for compressing a single file where zip can both compress files and join them together into an archive. For another, the gzip command zips \u201cin place\u201d. In other words, it leaves a compressed file \u2014 not the original file alongside the compressed copy. Here&#8217;s an example of gzip at work:<\/p>\n<pre>$ gzip onefile\r\n$ ls -l\r\n-rw-rw-r-- 1 shs shs 10514 Jan 15 13:13 onefile.gz\r\n<\/pre>\n<p>And here&#8217;s zip. Notice how this command requires that a name be provided for the zipped archive where gzip simply uses the original file name and adds the .gz extension.<\/p>\n<pre>$ zip twofiles.zip file*\r\n  adding: file1 (deflated 82%)\r\n  adding: file2 (deflated 82%)\r\n$ ls -l\r\n-rw-rw-r-- 1 shs shs 58021 Jan 15 13:25 file1\r\n-rw-rw-r-- 1 shs shs 58933 Jan 15 13:34 file2\r\n-rw-rw-r-- 1 shs shs 21289 Jan 15 13:35 twofiles.zip\r\n<\/pre>\n<p>Notice also that the original files are still sitting there.<\/p>\n<p>The amount of disk space that is saved (i.e., the degree of compression obtained) will depend on the content of each file. The variation in the example below is considerable.<\/p>\n<pre>$ zip mybin.zip ~\/bin\/*\r\n  adding: bin\/1 (deflated 26%)\r\n  adding: bin\/append (deflated 64%)\r\n  adding: bin\/BoD_meeting (deflated 18%)\r\n  adding: bin\/cpuhog1 (deflated 14%)\r\n  adding: bin\/cpuhog2 (stored 0%)\r\n  adding: bin\/ff (deflated 32%)\r\n  adding: bin\/file.0 (deflated 1%)\r\n  adding: bin\/loop (deflated 14%)\r\n  adding: bin\/notes (deflated 23%)\r\n  adding: bin\/patterns (stored 0%)\r\n  adding: bin\/runme (stored 0%)\r\n  adding: bin\/tryme (deflated 13%)\r\n  adding: bin\/tt (deflated 6%)\r\n<\/pre>\n<h2>The unzip command<\/h2>\n<p>The\u00a0<strong>unzip<\/strong>\u00a0command will recover the contents from a zip file and, as you&#8217;d likely suspect, leave the zip file intact, whereas a similar gunzip command would leave only the uncompressed file.<\/p>\n<aside id=\"\" class=\"nativo-promo nativo-promo-1 smartphone\"><\/aside>\n<pre>$ unzip twofiles.zip\r\nArchive:  twofiles.zip\r\n  inflating: file1\r\n  inflating: file2\r\n$ ls -l\r\n-rw-rw-r-- 1 shs shs 58021 Jan 15 13:25 file1\r\n-rw-rw-r-- 1 shs shs 58933 Jan 15 13:34 file2\r\n-rw-rw-r-- 1 shs shs 21289 Jan 15 13:35 twofiles.zip\r\n<\/pre>\n<h2>The zipcloak command<\/h2>\n<p>The\u00a0<strong>zipcloak<\/strong>\u00a0command encrypts a zip file, prompting you to enter a password twice (to help ensure you don&#8217;t &#8220;fat finger&#8221; it) and leaves the file in place. You can expect the file size to vary a little from the original.<\/p>\n<pre>$ zipcloak twofiles.zip\r\nEnter password:\r\nVerify password:\r\nencrypting: file1\r\nencrypting: file2\r\n$ ls -l\r\ntotal 204\r\n-rw-rw-r-- 1 shs shs 58021 Jan 15 13:25 file1\r\n-rw-rw-r-- 1 shs shs 58933 Jan 15 13:34 file2\r\n-rw-rw-r-- 1 shs shs 21313 Jan 15 13:46 twofiles.zip   &lt;== slightly larger than\r\n                                                           unencrypted version\r\n<\/pre>\n<p>Keep in mind that the original files are still sitting there unencrypted.<\/p>\n<h2>The zipdetails command<\/h2>\n<p>The\u00a0<strong>zipdetails<\/strong>\u00a0command is going to show you details \u2014 a\u00a0<i>lot<\/i>\u00a0of details about a zipped file, likely a lot more than you care to absorb. Even though we&#8217;re looking at an encrypted file, zipdetails does display the file names along with file modification dates, user and group information, file length data, etc. Keep in mind that this is all &#8220;metadata.&#8221; We don&#8217;t see the contents of the files.<\/p>\n<pre>$ zipdetails twofiles.zip\r\n\r\n0000 LOCAL HEADER #1       04034B50\r\n0004 Extract Zip Spec      14 '2.0'\r\n0005 Extract OS            00 'MS-DOS'\r\n0006 General Purpose Flag  0001\r\n     [Bit  0]              1 'Encryption'\r\n     [Bits 1-2]            1 'Maximum Compression'\r\n0008 Compression Method    0008 'Deflated'\r\n000A Last Mod Time         4E2F6B24 'Tue Jan 15 13:25:08 2019'\r\n000E CRC                   F1B115BD\r\n0012 Compressed Length     00002904\r\n0016 Uncompressed Length   0000E2A5\r\n001A Filename Length       0005\r\n001C Extra Length          001C\r\n001E Filename              'file1'\r\n0023 Extra ID #0001        5455 'UT: Extended Timestamp'\r\n0025   Length              0009\r\n0027   Flags               '03 mod access'\r\n0028   Mod Time            5C3E2584 'Tue Jan 15 13:25:08 2019'\r\n002C   Access Time         5C3E27BB 'Tue Jan 15 13:34:35 2019'\r\n0030 Extra ID #0002        7875 'ux: Unix Extra Type 3'\r\n0032   Length              000B\r\n0034   Version             01\r\n0035   UID Size            04\r\n0036   UID                 000003E8\r\n003A   GID Size            04\r\n003B   GID                 000003E8\r\n003F PAYLOAD\r\n\r\n2943 LOCAL HEADER #2       04034B50\r\n2947 Extract Zip Spec      14 '2.0'\r\n2948 Extract OS            00 'MS-DOS'\r\n2949 General Purpose Flag  0001\r\n     [Bit  0]              1 'Encryption'\r\n     [Bits 1-2]            1 'Maximum Compression'\r\n294B Compression Method    0008 'Deflated'\r\n294D Last Mod Time         4E2F6C56 'Tue Jan 15 13:34:44 2019'\r\n2951 CRC                   EC214569\r\n2955 Compressed Length     00002913\r\n2959 Uncompressed Length   0000E635\r\n295D Filename Length       0005\r\n295F Extra Length          001C\r\n2961 Filename              'file2'\r\n2966 Extra ID #0001        5455 'UT: Extended Timestamp'\r\n2968   Length              0009\r\n296A   Flags               '03 mod access'\r\n296B   Mod Time            5C3E27C4 'Tue Jan 15 13:34:44 2019'\r\n296F   Access Time         5C3E27BD 'Tue Jan 15 13:34:37 2019'\r\n2973 Extra ID #0002        7875 'ux: Unix Extra Type 3'\r\n2975   Length              000B\r\n2977   Version             01\r\n2978   UID Size            04\r\n2979   UID                 000003E8\r\n297D   GID Size            04\r\n297E   GID                 000003E8\r\n2982 PAYLOAD\r\n\r\n5295 CENTRAL HEADER #1     02014B50\r\n5299 Created Zip Spec      1E '3.0'\r\n529A Created OS            03 'Unix'\r\n529B Extract Zip Spec      14 '2.0'\r\n529C Extract OS            00 'MS-DOS'\r\n529D General Purpose Flag  0001\r\n     [Bit  0]              1 'Encryption'\r\n     [Bits 1-2]            1 'Maximum Compression'\r\n529F Compression Method    0008 'Deflated'\r\n52A1 Last Mod Time         4E2F6B24 'Tue Jan 15 13:25:08 2019'\r\n52A5 CRC                   F1B115BD\r\n52A9 Compressed Length     00002904\r\n52AD Uncompressed Length   0000E2A5\r\n52B1 Filename Length       0005\r\n52B3 Extra Length          0018\r\n52B5 Comment Length        0000\r\n52B7 Disk Start            0000\r\n52B9 Int File Attributes   0001\r\n     [Bit 0]               1 Text Data\r\n52BB Ext File Attributes   81B40000\r\n52BF Local Header Offset   00000000\r\n52C3 Filename              'file1'\r\n52C8 Extra ID #0001        5455 'UT: Extended Timestamp'\r\n52CA   Length              0005\r\n52CC   Flags               '03 mod access'\r\n52CD   Mod Time            5C3E2584 'Tue Jan 15 13:25:08 2019'\r\n52D1 Extra ID #0002        7875 'ux: Unix Extra Type 3'\r\n52D3   Length              000B\r\n52D5   Version             01\r\n52D6   UID Size            04\r\n52D7   UID                 000003E8\r\n52DB   GID Size            04\r\n52DC   GID                 000003E8\r\n\r\n52E0 CENTRAL HEADER #2     02014B50\r\n52E4 Created Zip Spec      1E '3.0'\r\n52E5 Created OS            03 'Unix'\r\n52E6 Extract Zip Spec      14 '2.0'\r\n52E7 Extract OS            00 'MS-DOS'\r\n52E8 General Purpose Flag  0001\r\n     [Bit  0]              1 'Encryption'\r\n     [Bits 1-2]            1 'Maximum Compression'\r\n52EA Compression Method    0008 'Deflated'\r\n52EC Last Mod Time         4E2F6C56 'Tue Jan 15 13:34:44 2019'\r\n52F0 CRC                   EC214569\r\n52F4 Compressed Length     00002913\r\n52F8 Uncompressed Length   0000E635\r\n52FC Filename Length       0005\r\n52FE Extra Length          0018\r\n5300 Comment Length        0000\r\n5302 Disk Start            0000\r\n5304 Int File Attributes   0001\r\n     [Bit 0]               1 Text Data\r\n5306 Ext File Attributes   81B40000\r\n530A Local Header Offset   00002943\r\n530E Filename              'file2'\r\n5313 Extra ID #0001        5455 'UT: Extended Timestamp'\r\n5315   Length              0005\r\n5317   Flags               '03 mod access'\r\n5318   Mod Time            5C3E27C4 'Tue Jan 15 13:34:44 2019'\r\n531C Extra ID #0002        7875 'ux: Unix Extra Type 3'\r\n531E   Length              000B\r\n5320   Version             01\r\n5321   UID Size            04\r\n5322   UID                 000003E8\r\n5326   GID Size            04\r\n5327   GID                 000003E8\r\n\r\n532B END CENTRAL HEADER    06054B50\r\n532F Number of this disk   0000\r\n5331 Central Dir Disk no   0000\r\n5333 Entries in this disk  0002\r\n5335 Total Entries         0002\r\n5337 Size of Central Dir   00000096\r\n533B Offset to Central Dir 00005295\r\n533F Comment Length        0000\r\nDone\r\n<\/pre>\n<h2>The zipgrep command<\/h2>\n<p>The\u00a0<strong>zipgrep<\/strong>\u00a0command is going to use a grep-type feature to locate particular content in your zipped files. If the file is encrypted, you will need to enter the password provided for the encryption for each file you want to examine. If you only want to check the contents of a single file from the archive, add its name to the end of the zipgrep command as shown below.<\/p>\n<pre>$ zipgrep hazard twofiles.zip file1\r\n[twofiles.zip] file1 password:\r\nCertain pesticides should be banned since they are hazardous to the environment.\r\n<\/pre>\n<h2>The zipinfo command<\/h2>\n<p>The\u00a0<strong>zipinfo<\/strong>\u00a0command provides information on the contents of a zipped file whether encrypted or not. This includes the file names, sizes, dates and permissions.<\/p>\n<pre>$ zipinfo twofiles.zip\r\nArchive:  twofiles.zip\r\nZip file size: 21313 bytes, number of entries: 2\r\n-rw-rw-r--  3.0 unx    58021 Tx defN 19-Jan-15 13:25 file1\r\n-rw-rw-r--  3.0 unx    58933 Tx defN 19-Jan-15 13:34 file2\r\n2 files, 116954 bytes uncompressed, 20991 bytes compressed:  82.1%\r\n<\/pre>\n<h2>The zipnote command<\/h2>\n<p>The\u00a0<strong>zipnote<\/strong>\u00a0command can be used to extract comments from zip archives or add them. To display comments, just preface the name of the archive with the command. If no comments have been added previously, you will see something like this:<\/p>\n<aside id=\"\" class=\"nativo-promo nativo-promo-2 tablet desktop smartphone\"><\/aside>\n<pre>$ zipnote twofiles.zip\r\n@ file1\r\n@ (comment above this line)\r\n@ file2\r\n@ (comment above this line)\r\n@ (zip file comment below this line)\r\n<\/pre>\n<p>If you want to add comments, write the output from the zipnote command to a file:<\/p>\n<pre>$ zipnote twofiles.zip &gt; comments\r\n<\/pre>\n<p>Next, edit the file you&#8217;ve just created, inserting your comments above the\u00a0<strong>(comment above this line)<\/strong>\u00a0lines. Then add the comments using a zipnote command like this one:<\/p>\n<pre>$ zipnote -w twofiles.zip &lt; comments\r\n<\/pre>\n<h2>The zipsplit command<\/h2>\n<p>The\u00a0<strong>zipsplit<\/strong>\u00a0command can be used to break a zip archive into multiple zip archives when the original file is too large \u2014 maybe because you&#8217;re trying to add one of the files to a small thumb drive. The easiest way to do this seems to be to specify the max size for each of the zipped file portions. This size must be large enough to accomodate the largest included file.<\/p>\n<pre>$ zipsplit -n 12000 twofiles.zip\r\n2 zip files will be made (100% efficiency)\r\ncreating: twofile1.zip\r\ncreating: twofile2.zip\r\n$ ls twofile*.zip\r\n-rw-rw-r-- 1 shs shs  10697 Jan 15 14:52 twofile1.zip\r\n-rw-rw-r-- 1 shs shs  10702 Jan 15 14:52 twofile2.zip\r\n-rw-rw-r-- 1 shs shs  21377 Jan 15 14:27 twofiles.zip\r\n<\/pre>\n<p>Notice how the extracted files are sequentially named &#8220;twofile1&#8221; and &#8220;twofile2&#8221;.<\/p>\n<h2>Wrap-up<\/h2>\n<p>The\u00a0<strong>zip<\/strong>\u00a0command, along with some of its zipping compatriots, provide a lot of control over how you generate and work with compressed file archives.<\/p>\n<\/div>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=265042\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are quite a few interesting things that you can do with &#8220;zip&#8221; commands other than compress and uncompress files. Here are some other zip options and how they can help. Some of us have been zipping files on Unix and Linux systems for many decades \u2014 to save some disk space and package files &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/17\/zipping-files-on-linux-the-many-variations-and-how-to-use-them\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Zipping files on Linux: the many variations and how to use them&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8265","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=8265"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8265\/revisions"}],"predecessor-version":[{"id":10759,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8265\/revisions\/10759"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=8265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=8265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=8265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}