List files in zip subdirectory - excel

To list all the files in a spreadsheet (xlsx) file, I can do:
$ unzip -l /Users/david/Desktop/myspreadsheet.xlsx
698 01-01-1980 00:00 xl/_rels/workbook.xml.rels
1415625191 01-01-1980 00:00 xl/worksheets/sheet1.xml
6798 01-01-1980 00:00 xl/theme/theme1.xml
2315 01-01-1980 00:00 xl/styles.xml
779218 01-01-1980 00:00 xl/sharedStrings.xml
322 01-01-1980 00:00 xl/worksheets/_rels/sheet1.xml.rels
9840 01-01-1980 00:00 xl/printerSettings/printerSettings1.bin
640 01-01-1980 00:00 docProps/core.xml
797 01-01-1980 00:00 docProps/app.xml
From here we can see that the spreadsheet has one sheet -- xl/worksheets/sheet1.xml. Is there a way to only see the zip contents of the xl/worksheets/ folder? For example, doing something like:
$ unzip -l /Users/david/Desktop/myspreadsheet.xlsx xl/worksheets
The most I could find from man was:
-l list archive files (short format). The names, uncompressed file sizes and modification dates and
times of the specified files are printed, along with totals for all files specified. If UnZip was
compiled with OS2_EAS defined, the -l option also lists columns for the sizes of stored OS/2
extended attributes (EAs) and OS/2 access control lists (ACLs). In addition, the zipfile comment
and individual file comments (if any) are displayed. If a file was archived from a single-case
file system (for example, the old MS-DOS FAT file system) and the -L option was given, the file-
name is converted to lowercase and is prefixed with a caret (^).
But it seems like there are tons of other options. Is there a way to do the above?

Related

How to preserve timestamp of original file post zip compression?

I have a lot of files on our servers which we compression with a filter that only the files older than x days will get compressed.
The zip command compresses the original, makes a filename.zip and removes the original.
This has a small problem that the timestamp changes since the compression job runs after x days.
So when we run files to remove older files (which are by now zip files), not all files get removed since the timestamp has changed from the original file to the compressed file.
I would like to add a condition where while zipping, i want the original timestamp of the file to be retained by the zip archive even though its running at a later date.
One way of doing this would be to
Get timestamp of each original file with a date command
Compress the original, remove the original
Use and insert the earlier stored timestamp to the new zip file using "touch"
I am looking for a simpler solution.
Some old file I had:
$ ls -l foo
-rw-r--r-- 1 james james 120 Sep 5 07:28 foo
Zip and redate:
$ zip foo.zip foo && touch -d "$(date -R -r foo)" foo.zip
Check it out:
$ ls -l foo.zip
-rw-r--r-- 1 james james 120 Sep 5 07:28 foo.zip
Remove the original:
$ rm -i foo
Yes you can unzip a file and preserve the old timestamp from the original time it was created. Steps to do this are as below:
Click on the filename.zip, properties
In the General tab, the security says "This file came from another computer and might be blocked to help protect this computer". Click on the Unblock check box and click OK
Extract the file and volla, the extracted file has the datatime stamp when the file was created/modified

Run script on specific file in all subdirs

I've written a script (foo) which makes a simple sed replacement on text in the input file. I have a directory (a) containing a large number of subdirectories (a/b1, a/b2 etc) which all have the same subdirs (c, etc) and contain a file with the same name (d). So the rough structure is:
a/
-b1/
--c/
---d
-b2/
--c/
---d
-b3/
--c/
---d
I want to run my script on every file (d) in the tree. Unfortunately the following doesn't work:
sudo sh foo a/*/c/d
how do I use wildcards in a bash command like this? Do I have to use find with specific max and mindepth, or is there a more elegant solution?
The wildcard expansion in your example should work, and no find should be needed. I assume a b and c are just some generic file names to simplify the question. Do any of your folders/files contain spaces?
If you do:
ls -l a/*/d/c
are you getting the files you need listed? If so, then it is how you handle the $* in your script file. Mind sharing it with us?
As you can see, wildcard expansion works
$ ls -l a/*/c/d
-rw-r--r-- 1 user wheel 0 15 Apr 08:05 a/b1/c/d
-rw-r--r-- 1 user wheel 0 15 Apr 08:05 a/b2/c/d
-rw-r--r-- 1 user wheel 0 15 Apr 08:05 a/b3/c/d

How to create zip file deflate all the entry

I have a zip file that i think it was created by zip, but when i unzip it, and try to create a zip from those unziped file, it diferent from zipinfo command.
this is result when I do zipinfo command on linux of source file:
root#TLBBServer:/home/web/Patch/u# zipinfo u1.zip
Archive: u1.zip
Zip file size: 662378 bytes, number of entries: 3
-rw-a-- 2.3 ntf 3133736 bx defX 08-Jan-28 07:20 Data/LaunchSkin.axp
-rw-a-- 2.3 ntf 1196154 bx defX 08-Feb-03 03:52 (version)
-rw-a-- 2.3 ntf 36 bx defX 08-Feb-03 03:53 (command)
3 files, 4329926 bytes uncompressed, 661972 bytes compressed: 84.7%
this is result when i do with a clone file:
root#TLBBServer:/home/web/Patch/u# zipinfo u.zip
Archive: u.zip
Zip file size: 661897 bytes, number of entries: 3
-rw---- 2.3 ntf 1217589 tx defX 08-Feb-03 03:52 (version)
-rw---- 2.3 ntf 3135715 bx defX 08-Jan-28 07:20 Data/LaunchSkin.axp
-rw---- 2.3 ntf 38 tx stor 08-Feb-03 03:53 (command)
3 files, 4353342 bytes uncompressed, 661255 bytes compressed: 84.8%
this is source file:
https://github.com/HadesD/TLBB-Web/raw/master/u1.zip
So? Why do you care?
A different zip program, or the same zip program with different settings, or a different version of the same zip program with the same settings can all produce different output. However they will all be valid, decompressible zip files, assuring that the contents are uncompressed exactly.
The only guarantee of the lossless compressor is that compression followed by decompression will always give you exactly the same thing back. There is no guarantee, and there does not need to be a guarantee, that decompression followed by compression will give you the same thing back.

How to validate equivalency between two zip packages

I'm trying to validate if two zip packages are equivalent. I can not rely on md5sum. When I extract the two packages, and do a md5sum diff between all the files in the packages, there is no difference, and all files have equivalent md5sums. But the zip packages themselves have different md5sum values. My question is: How can I validate that two zip packages are equivalent?
When you list the archive's content with
unzip -v archive.zip
you get a list of files with these column headings
Length Method Size Cmpr Date Time CRC-32 Name
Depending on what you consider equivalent (e.g. Size, CRC, Name), you can extract the relevant columns for both archives, sort them and do a diff over the output.
without unzipping the file you can use zipinfo
e.g:
ipinfo 5.zip
Archive: 5.zip 158 bytes 1 file
drwxr-xr-x 3.0 unx 0 bx stor 18-Nov-13 07:23 501/
1 file, 0 bytes uncompressed, 0 bytes compressed: 0.0%

Linux: Unzip an archive containing files with the same name

I was sent a zip file containing 40 files with the same name.
I wanted to extract each of these files to a seperate folder OR extract each file with a different name (file1, file2, etc).
Is there a way to do this automatically with standard linux tools? A check of man unzip revealed nothing that could help me. zipsplit also does not seem to allow an arbitrary splitting of zip files (I was trying to split the zip into 40 archives, each containing one file).
At the moment I am (r)enaming my files individually. This is not so much of a problem with a 40 file archive, but is obviously unscalable.
Anyone have a nice, simple way of doing this? More curious than anything else.
Thanks.
Assuming that no such tool currently exists, then it should be quite easy to write one in python. Python has a zipfile module that should be sufficient.
Something like this (maybe, untested):
#!/usr/bin/env python
import os
import sys
import zipfile
count = 0
z = zipfile.ZipFile(sys.argv[1],"r")
for info in z.infolist():
directory = str(count)
os.makedirs(directory)
z.extract(info,directory)
count += 1
z.close()
I know this is a couple years old, but the answers above did not solve my particular problem here so I thought I should go ahead and post a solution that worked for me.
Without scripting, you can just use command line input to interact with the unzip tools text interface. That is, when you type this at the command line:
unzip file.zip
and it contains files of the same name, it will prompt you with:
replace sameName.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:
If you wanted to do this by hand, you would type "r", and then at the next prompt:
new name:
you would just type the new file name.
To automate this, simply create a text file with the responses to these prompts and use it as the input to unzip, as follows.
r
sameName_1.txt
r
sameName_2.txt
...
That is generated pretty easily using your favorite scripting language. Save it as unzip_input.txt and then use it as input to unzip like this:
unzip < unzip_input.txt
For me, this was less of a headache than trying to get the Perl or Python extraction modules working the way I needed. Hope this helps someone...
here is a linux script version
in this case the 834733991_T_ONTIME.csv is the name of the file that is the same inside every zip file, and the .csv after "$count" simply has to be swapped with the file type you want
#!/bin/bash
count=0
for a in *.zip
do
unzip -q "$a"
mv 834733991_T_ONTIME.csv "$count".csv
count=$(($count+1))
done`
This thread is old but there is still room for improvement. Personally I prefer the following one-liner in bash
unzipd ()
{
unzip -d "${1%.*}" "$1"
}
Nice, clean, and simple way to remove the extension and use the
Using unzip -B file.zip did the trick for me. It creates a backup file suffixed with ~<number> in case the file already exists.
For example:
$ rm *.xml
$ unzip -B bogus.zip
Archive: bogus.zip
inflating: foo.xml
inflating: foo.xml
inflating: foo.xml
inflating: foo.xml
inflating: foo.xml
$ ls -l
-rw-rw-r-- 1 user user 1161 Dec 20 20:03 bogus.zip
-rw-rw-r-- 1 user user 1501 Dec 16 14:34 foo.xml
-rw-rw-r-- 1 user user 1520 Dec 16 14:45 foo.xml~
-rw-rw-r-- 1 user user 1501 Dec 16 14:47 foo.xml~1
-rw-rw-r-- 1 user user 1520 Dec 16 14:53 foo.xml~2
-rw-rw-r-- 1 user user 1520 Dec 16 14:54 foo.xml~3
Note: the -B option does not show up in unzip --help, but is mentioned in the man pages: https://manpages.org/unzip#options

Resources