3

I got a zip file of ~400MB and within it there are certain txt/json files roughly in the range of 400MB - 1GB size.

I simply want to view first 100 lines of a target file without extracting it from the zip, without swamping out my memory.(very low)

Alternatively is there an option to split the target file in the zip, without extracting and doing it sequentially, i.e, when giving the split command with 50MB for eg, I only get the first split file, view it, then, continue the split for a second file, view it, continue for a third 50MB file, view it, again and again?

1
  • How much RAM? How much swap? Should be easy... open the zip file with the Archive Manager, and double-click on any txt/json file you wish to look at. Let me know if that works for you and I can make an answer for you.
    – heynnema
    Commented Aug 18, 2017 at 15:49

1 Answer 1

7

Ok the steps would be:

  1. To get the name of the file in archive you wish to view it's content, list out the content (of the zip file ) with this command:

    unzip -l file.zip
    
  2. Now to view the first 100 lines:

    unzip -c file.zip file/name_of_file_of_interest.json | head -100
    

Note:

  1. This format

    unzip -c file.zip file/name_of_file_of_interest.json
    

    is needed, not

    unzip -c file.zip name_of_file_of_interest.json
    

    to get to the path to the file and this will be gotten when the command in step 1 is run.

Example:

  1. File unzip -l wordpress-4.7.zip, produces

     ...
     0  2016-12-06 19:40   wordpress/wp-includes/Text/Diff/Renderer/
     5535  2010-02-19 02:25   wordpress/wp-includes/Text/Diff/Renderer/inline.php
     6813  2015-06-28 16:27   wordpress/wp-includes/Text/Diff/Renderer.php
    12950  2015-06-28 16:27   wordpress/wp-includes/Text/Diff.php
    14460  2016-12-02 08:11   wordpress/wp-includes/class-wp-hook.php
    34760  2016-12-01 03:19   wordpress/wp-includes/rest-api.php
    23621  2016-11-21 03:46   wordpress/wp-includes/update.php
    102116  2016-12-04 21:37   wordpress/wp-includes/comment.php
    14066  2016-08-25 18:37   wordpress/wp-includes/class-wp-text-diff-renderer-table.php
    2853  2015-12-16 10:58   wordpress/wp-config-sample.php
    
  2. Now I want to see first 100 lines of the file wp-config-sample.php in wordpress-4.7.zip, so I do:

    unzip -p wordpress-4.7.zip wordpress/wp-config-sample.php | head -100
    

More information:

  1. Why use -p option:

     -p     extract files to pipe (stdout).  Nothing but the file data is sent to stdout, and the files are  always
          extracted in binary format, just as they are stored (no conversions).
    

A more elagant output would be with less

less wordpress-4.7.zip

# OUTPUT

    Archive:  wordpress-4.7.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Stored        0   0% 2016-12-06 19:40 00000000  wordpress/
   16250  Defl:X     4157  74% 2016-11-29 06:39 2b3bf692  wordpress/wp-settings.php
    3286  Defl:X     1397  58% 2015-05-24 18:26 e732ef29  wordpress/wp-cron.php
    1627  Defl:X      799  51% 2016-08-29 13:00 d0339e89  wordpress/wp-comments-post.php
    5447  Defl:X     2006  63% 2016-09-27 22:36 888ccd8b  wordpress/wp-activate.php
       0  Stored        0   0% 2016-12-06 19:40 00000000  wordpress/wp-admin/
    2401  Defl:X      933  61% 2016-10-24 05:45 1e03d7a9  wordpress/wp-admin/link-parse-opml.php
       0  Stored        0   0% 2016-12-06 19:40 00000000  wordpress/wp-admin/js/
   12000  Defl:X     3291  73% 2016-09-13 07:03 77f8b8cb  wordpress/wp-admin/js/editor.js
    6224  Defl:X     2215  64% 2016-11-03 06:40 693a34f0  wordpress/wp-admin/js/user-profile.min.js
    1508  Defl:X      640  58% 2015-07-27 12:19 ee45bde7  wordpress/wp-admin/js/word-count.min.js
    5148  Defl:X     2010  61% 2016-11-17 19:28 2384dae7  wordpress/wp-admin/js/tags-suggest.js
    6842  Defl:X     2399  65% 2016-10-23 19:16 41742fcd  wordpress/wp-admin/js/tags-box.js
   17717  Defl:X     5102  71% 2016-07-05 14:37 93dfd6b0  wordpress/wp-admin/js/image-edit.js
   34299  Defl:X     7077  79% 2016-11-04 18:18 97b89baa  wordpress/wp-admin/js/updates.min.js
    4192  Defl:X     1539  63% 2016-06-08 23:37 b8c4c822  wordpress/wp-admin/js/postbox.min.js
   20983  Defl:X     6187  71% 2016-11-03 06:40 3aec32c2  wordpress/wp-admin/js/nav-menu.min.js
   25330  Defl:X     7009  72% 2016-11-03 06:40 43591b6e  wordpress/wp-admin/js/theme.min.js
    1502  Defl:X      707  53% 2013-11-14 06:40 4dc0acc0  wordpress/wp-admin/js/custom-header.js
   11462  Defl:X     4344  62% 2016-11-03 06:40 0aa95325  wordpress/wp-admin/js/press-this.min.js
10
  • 1
    -p might be a better option that -c when viewing a single file.
    – muru
    Commented Aug 18, 2017 at 7:59
  • It's amazing how unzip "knows" when to stop decompressing.
    – pLumo
    Commented Aug 18, 2017 at 8:00
  • 1
    @RoVo I doubt it knows. It probably gets killed when head finishes reading and terminates, closing the pipe.
    – muru
    Commented Aug 18, 2017 at 8:05
  • That's why i put " ;-)
    – pLumo
    Commented Aug 18, 2017 at 8:06
  • What @muru said is how pipes normally work on *nix systems. It's just that most of the time, the right-hand side of the pipe consumes everything from the left-hand side.
    – user
    Commented Aug 18, 2017 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .