244

How can I read the contents of a particular file in an archive without extracting the .zip it is contained within? I'm using the Linux command line.

An earlier question asks about viewing the directory of the archive. But for me it is not enough to see just a list of the files in the archive, I need to see the contents of a file in the archive.

kasperd
  • 2,861
  • 1
  • 18
  • 29
Shrikanth
  • 2,441
  • 2
  • 14
  • 3
  • 2
    Duplicity with http://superuser.com/questions/216617/zip-linux-view-files-in-archive – pevik Jul 03 '14 at 12:37
  • @fixer1234 (and others): [The linked question](//superuser.com/q/216617/150988) asks, “How can I view the files in a ZIP archive?” AFAIC, that’s the same question as “How can I see the contents of a file …?” It’s unfortunate that many of the people who answered that question *interpreted it* as “How can I view the directory of the archive?” However, [Gilles’s answer](//superuser.com/q/216617/150988#216930) (naturally) and [Rajasekhar Tolety’s answer](//superuser.com/q/216617/150988#689228) (apparently) to that question provide answers to this question. – Scott - Слава Україні Apr 21 '17 at 19:42
  • 1
    @Scott, maybe we should figure out how to merge the two questions so both topics are covered in one, or refocus the other to clearly be about the directory and then move answers between both places to match the questions. Right now, both are a mishmash. – fixer1234 Apr 21 '17 at 20:12
  • @fixer1234: I agree, up to a point. The moderators are always telling us that duplicates are a good thing, because they provide a greater surface of exposure to the search engines (i.e., more chances that a search will find one of the questions). But there’s the rub: if a user finds *one* of the questions, and the linkage isn’t obvious (and nobody looks at the lists of “Linked” and “Related” questions — at least not random followers of search results), then the user has found only a fraction of the answers. DavidPostill cast the final vote to reopen this question; maybe you should talk to him. – Scott - Слава Україні Apr 21 '17 at 20:26
  • @Scott — Given that the question asker marked the “How can I view the directory of the archive?” answer as accepted, I have to think that was likely the intent of the question. It is, unfortunately, ambiguously phrased such that it could mean either interpretation. – M. Justin Apr 21 '17 at 20:38
  • @M.Justin: I’m not sure whether that’s really relevant.  If there are answers in *that* other thread that answer *this* question, then *this* question can/should be closed as a duplicate of *that* one.  See the boilerplate text (you’ll have to find another question that’s closed as a duplicate, since this one has been reopened): “**This question already has an answer here:**” – Scott - Слава Україні Apr 21 '17 at 20:57
  • @Scott — That all said, there's currently a meta question open discussing this pair of issues, in case you were unaware: https://meta.superuser.com/questions/12356/are-these-two-questions-about-viewing-zip-file-contents-actually-duplicates – M. Justin Apr 21 '17 at 21:00
  • 'zcat' or 'zless' do this for gzipped files. They might also work with zips – Brad May 01 '17 at 11:56

8 Answers8

307

unzip -l archive.zip lists the contents of a ZIP archive to ensure your file is inside.

Use the -p option to write the contents of named files to stdout (screen) without having to uncompress the entire archive.

unzip -p archive.zip file1.txt | less

For this kind of operation I always pipe the output to less, otherwise the whole file goes flying up the screen before you can read it.

BTW zcat is great for viewing the contents of .gz files without having to uncompress them first.

Edit: Changed this answer to use -p instead of -c. -p extracts the file byte-for-byte, while -c prints the filename and may do EOL conversion. Also, unzip -p lets you extract multiple files, but it does not output in the order given like cat does.

Joey Adams
  • 2,278
  • 3
  • 21
  • 25
danielcraigie
  • 3,411
  • 1
  • 14
  • 10
  • 10
    At least in Ubuntu, less is somehow configured to do this by default - so `less archive.zip` actually shows the list of files. – aviv Mar 24 '13 at 20:30
  • 1
    bzcat for viewing the contents of bz2 files without having to uncompress them first... – Justin E Nov 12 '14 at 19:37
  • 5
    unzip -c also shows the file name and some extra messages (sending them to stdout). Using -p instead only sends the file in binary format. That's more useful for piping – Juan Calero Mar 24 '15 at 10:02
  • With your `$LESSOPEN` set to lesspipe, you can just `less foo.gz` to view the decompressed contents. (@aviv: this is the same thing that enables `less foo.zip` to pipe `unzip -l foo.zip` into itself). – Peter Cordes Jan 04 '17 at 11:57
  • `unzip -c archive.zip '*' | less` does what I was looking for: cat the contents of the zip archive. – Peter Cordes Jan 04 '17 at 11:59
  • If the output of less is to be written to a text file, what can be the command? – arjun Aug 18 '17 at 07:22
  • @arjun have a look at https://askubuntu.com/a/420983 to see options for writing command output to files. If you still need to monitor what is being written then pipe to 'tee' instead of less. – danielcraigie Sep 02 '17 at 15:24
  • 3
    `unzip -p ...` will cat the contents of the files in the archive *without* inserting the file names into the output stream – jvd10 May 24 '19 at 21:19
  • @danielcraigie: I changed the answer to recommend `-p` instead of `-c` to avoid data corruption for some use cases. On Windows at least, `unzip -c` automatically performs EOL conversion. Please review to see if you agree. – Joey Adams Aug 02 '20 at 17:21
10

You can use vim to list content of the zip/rar/tar archive:

vim archive.zip

BTW: here is the same question.

patryk.beza
  • 1,521
  • 5
  • 21
  • 31
9

zipinfo is another tool you might use, this is useful if you're on a locked-down system where unzip is not allowed.

Jens Erat
  • 17,507
  • 14
  • 61
  • 74
Nickolai
  • 191
  • 1
  • 2
4

If you're just looking to view images inside the archives, you can use Comix or newer MComix to see images inside .zip, .rar, .cbr, and .cbz files without extracting.

Kokizzu
  • 1,715
  • 3
  • 16
  • 26
2

Start Emacs in command-line and open your zip files with Zip-Archive mode. Without any Emacs/elisp tuning (new users generally fear about), you will see file details like from zipinfo: modes, length, date, time

Then, you will be able to open files in buffers and even save your changes back to archive, with standard shortcuts:

  • Enter on a file name in list to open it
  • Edit and save with Ctrl-x Ctrl-s
  • Kill buffer Ctrl-k to go back to archive buffer and go on

When in Zip-Archive buffer, use Ctrl-h m to get all shortcuts available in Help View.

Hope this may lead you to discover Emacs awesome features

Yves Martin
  • 552
  • 7
  • 13
1

A better way is just using zmore or zless, for example

zmore syslog.2.gz
zx485
  • 2,170
  • 11
  • 17
  • 24
0

I've found less archive.zip to be the easiest way to do this.

RobertMyles
  • 101
  • 2
-1

If the file is included in zip archive, that you need to extract only that file from archive (may depend on archive type, some archives can't extract files seperately)

blogger
  • 600
  • 2
  • 6