Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to pull a single file from a ZIP file which I know the path to. Is there a command like the following:

unzip -d . myarchive.zip path/to/zipped/file.txt

Unfortunately, the above command extracts and recreates the entire path to the file at ./path/to/zipped/file.txt. Is there a way for me to simply pull the file out into a specified directory?

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

You can extract just the text to standard output with the -p option:

unzip -p myarchive.zip path/to/zipped/file.txt >file.txt

This won't extract the metadata (date, permissions, …), only the file contents. That's the price to pay for the convenience of not having to move the file afterwards.

Alternatively, mount the archive as a directory and just copy the file. With AVFS:

mountavfs
cp -p ~/.avfs"$PWD/myarchive.zip#"/path/to/zipped/file.txt .

Or with fuse-zip:

mkdir myarchive.d
fuse-zip myarchive.zip myarchive.d
cp -p myarchive.d/path/to/zipped/file.txt .
fusermount -u myarchive.d; rmdir myarchive.d
share|improve this answer
    
Will it work for binary files, say a jar file? –  Naftuli Tzvi Kay Jun 1 '11 at 1:01
    
@TK Kocheran: Why don't you just test it? I tested first command, and it worked. –  user unknown Jun 1 '11 at 3:09
    
@TKKocheran: Jar files are zips, so the unzip and fuse-zip methods will obviously work. The AVFS method also works, because AVFS guesses the format based on file names and knows about .jar; if your file is named differently you might need to tell AVFS to use its zip handler, e.g. ~/.avfs$PWD/foo.apk#uzip/META-INF. –  Gilles Jun 1 '11 at 7:19
    
actually, I meant extracting a JAR/binary file from a ZIP archive. I haven't had the chance to test it out yet, can you see any issues using the first command above to extract binary files? –  Naftuli Tzvi Kay Jun 1 '11 at 18:41
2  
@TKKocheran: There's no problem, -p extracts the file as-is (-c does text conversion). –  Gilles Jun 1 '11 at 20:50
show 1 more comment

This job is a lot simpler. just try the below:

unzip myarchive.zip in/archive/file.txt

this will create one directory in/archive/ in the present working directory and inside that you'll find file.txt extracted. you've to put the zipped view of the desired file. u can check that easily with unzip -l myarchive.zip first. Voila!!!

share|improve this answer
add comment
unzip -j "myarchive.zip" "in/archive/file.txt" -d "/path/to/unzip/to"

Enter full path for zipped file, not just the filename. Be sure to keep the structure as seen from within the zip file.

This will extract the single file file.txt in myarchive.zip to /path/to/unzip/to/file.txt.

share|improve this answer
    
This is the better answer. –  redburn Feb 24 at 14:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.