Read the contents of a zipped file without extraction?

163

26

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.

Shrikanth

Posted 2012-08-17T10:41:11.970

Reputation: 1 631

1

Duplicity with http://superuser.com/questions/216617/zip-linux-view-files-in-archive

– pevik – 2014-07-03T12:37:27.690

@fixer1234 (and others): The linked question 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 (naturally) and Rajasekhar Tolety’s answer (apparently) to that question provide answers to this question.

– Scott – 2017-04-21T19:42:09.533

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 – 2017-04-21T20:12:58.357

@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 – 2017-04-21T20:26:13.887

@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 – 2017-04-21T20:38:50.793

@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 – 2017-04-21T20:57:35.950

@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 – 2017-04-21T21:00:33.003

'zcat' or 'zless' do this for gzipped files. They might also work with zips – Brad – 2017-05-01T11:56:46.643

Answers

213

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

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

unzip -c archive.zip file1.txt file2.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.

danielcraigie

Posted 2012-08-17T10:41:11.970

Reputation: 2 451

1bzcat for viewing the contents of bz2 files without having to uncompress them first... – Justin E – 2014-11-12T19:37:03.300

3unzip -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 – 2015-03-24T10:02:48.167

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 – 2017-01-04T11:57:54.827

unzip -c archive.zip '*' | less does what I was looking for: cat the contents of the zip archive. – Peter Cordes – 2017-01-04T11:59:36.137

If the output of less is to be written to a text file, what can be the command? – arjun – 2017-08-18T07:22:44.833

@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 – 2017-09-02T15:24:42.070

1unzip -p ... will cat the contents of the files in the archive without inserting the file names into the output stream – jvd10 – 2019-05-24T21:19:08.980

8At least in Ubuntu, less is somehow configured to do this by default - so less archive.zip actually shows the list of files. – aviv – 2013-03-24T20:30:40.247

5

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

Nickolai

Posted 2012-08-17T10:41:11.970

Reputation: 151

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

Posted 2012-08-17T10:41:11.970

Reputation: 1 205

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

Posted 2012-08-17T10:41:11.970

Reputation: 522

1

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

vim archive.zip

BTW: here is the same question.

patryk.beza

Posted 2012-08-17T10:41:11.970

Reputation: 1 131

1

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

zmore syslog.2.gz

user1137450

Posted 2012-08-17T10:41:11.970

Reputation: 11

0

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

RobertMyles

Posted 2012-08-17T10:41:11.970

Reputation: 101

2less archive.zip doesn't show the contents of a zipped file, it only shows the contents of a zipped archive. – karel – 2019-04-25T11:16:42.493

Thanks for the clarification, @karel – RobertMyles – 2019-04-25T11:21:07.980

I think that what you're really trying to do would be the same as danielcraigie's answer. – karel – 2019-04-25T11:22:21.557

-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

Posted 2012-08-17T10:41:11.970

Reputation: 590

Yes, but you can extract to stdout and pipe right into a pager. – Peter Cordes – 2017-01-04T12:08:33.750

This is not a technical answer, it is more likely from a politician or a solicitor. Please be more specific! (show the command for example) – Hola Soy Edu Feliz Navidad – 2018-08-21T15:56:16.933