[Solved] Create an Empty Zip File Via The Terminal

About writing shell scripts and making the most of your shell
Forum rules
Before you post please read how to get help
Post Reply
mikeflan
Level 11
Level 11
Posts: 3719
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

[Solved] Create an Empty Zip File Via The Terminal

Post by mikeflan »

This is kinda hard to believe. Apparently the maintainer of the zip command did not foresee anyone wanting to create an empty zip file. :roll:
One would think

Code: Select all

zip zipname.zip
might do it, but it doesn't and gives an error.

Maybe something like

Code: Select all

zip --empty zipname.zip
but they didn't create that either.

So people are doing this:

Code: Select all

echo | zip -q > zipname.zip  && zip -dq zipname.zip -
Taken from here:
https://stackoverflow.com/questions/585 ... -in-ubuntu

I challenge you to come up with a better way, and more importantly convince the zip maintainer to add this capability.
I am working a job now, so I won't be responding as fast. If you need a speedy response from me please quote my post. I will try to get to those a little quicker. Thank you.
User avatar
xenopeek
Level 25
Level 25
Posts: 27361
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: [Solved] Create an Empty Zip File Via The Terminal

Post by xenopeek »

Could you set the scene as for why you want a zip file with no files in it? :)

Use 7-Zip (the official Linux build from https://www.7-zip.org/):
7zz a empty1.zip @/dev/null

Or p7zip (the old Linux clone of 7-Zip):
7z a -i@/dev/null empty2.zip

Or use Python 3:
python3 -m zipfile -c empty3.zip
That works on every system with Python 3 installed (it also works with Python 2). It may be the preferred solution because nothing else needs to be installed or depended on.

All these create an empty zip file that is identical to the zip file your command created. (md5sum = 76cdb2bad9582d23c1f6f4d868218d6c)
Image
t42
Level 8
Level 8
Posts: 2029
Joined: Mon Jan 20, 2014 6:48 pm

Re: [Solved] Create an Empty Zip File Via The Terminal

Post by t42 »

(md5sum = 76cdb2bad9582d23c1f6f4d868218d6c)

as any zip archive after zip -d archive_name *

Code: Select all

touch 333
zip 333.zip 333 &&  zip -d 333.zip *
md5sum 333.zip
76cdb2bad9582d23c1f6f4d868218d6c  333.zip
more importantly convince the zip maintainer to add this capability
Such archives produce various errors in many programs, for example, Xarchiver :
An error occurred while accessing the archive
also read zip -L
-=t42=-
mikeflan
Level 11
Level 11
Posts: 3719
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: [Solved] Create an Empty Zip File Via The Terminal

Post by mikeflan »

I am using gdal to convert a whole bunch of pdf files to tif. That process takes a long time (about 1 - 5 minutes each) and I had a hundred or so to do. The pdf and tif are the same file name. I wanted to create an empty zip file to zip the tif into. The pdf gets thrown away. I zip the tif via Nemo gui.

Yes, I could have just created terminal commands to create the zip files while populating them. But I chose to do the work earlier while gdal was running, and the zip files going away (being moved to another directory after being populated) helped me keep track of where I was in the process.

This one worked perfectly:

Code: Select all

$ 7z a -i@/dev/null empty2.zip

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,6 CPUs AMD FX(tm)-6200 Six-Core Processor              (600F12),ASM,AES-NI)

Scanning the drive:
0 files, 0 bytes

Creating archive: empty2.zip

Items to compress: 0

    
Files read from disk: 0
Archive size: 22 bytes (1 KiB)
Everything is Ok
$
And is perhaps better than the one I posted.


This one did not work on my LM 19.3:

Code: Select all

$ 7zz a empty1.zip @/dev/null

Command '7zz' not found, did you mean:

  command '7z' from deb p7zip-full
  command 'bzz' from deb djvulibre-bin
  command 'ezz' from deb fex-utils
  command 'zz' from deb fex-utils
  command '7zr' from deb p7zip
  command '7za' from deb p7zip-full

Try: sudo apt install <deb name>

$
I don't see 7zz in my Software Manager.


And this one did not work on my LM 19.3:

Code: Select all

$ python3 -m zipfile -c empty3.zip
Usage:
    zipfile.py -l zipfile.zip        # Show listing of a zipfile
    zipfile.py -t zipfile.zip        # Test if a zipfile is valid
    zipfile.py -e zipfile.zip target # Extract zipfile into target dir
    zipfile.py -c zipfile.zip src ... # Create zipfile from sources

$
I did read
zip -L

Thanks for all the help.
I am working a job now, so I won't be responding as fast. If you need a speedy response from me please quote my post. I will try to get to those a little quicker. Thank you.
User avatar
xenopeek
Level 25
Level 25
Posts: 27361
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: [Solved] Create an Empty Zip File Via The Terminal

Post by xenopeek »

mikeflan wrote:
Sun Jul 31, 2022 8:40 pm
This one did not work on my LM 19.3:

Code: Select all

$ 7zz a empty1.zip @/dev/null
I know. It's a manual download and manual install. That's why I linked to the website.
mikeflan wrote:
Sun Jul 31, 2022 8:40 pm
And this one did not work on my LM 19.3:

Code: Select all

$ python3 -m zipfile -c empty3.zip
Right. I overestimated that zipfile would work the same for very old versions. On Linux Mint 21 it will work.
Image
mikeflan
Level 11
Level 11
Posts: 3719
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: [Solved] Create an Empty Zip File Via The Terminal

Post by mikeflan »

You must admit this is a bit confusing. I can run the 7z command. man 7z suggests I have 7-zip installed. But to run 7zz you point to a website for 7-zip :shock: I will assume the 7-zip at the website would extend the capability of my 7-zip, or at least install a 7-zip with the extended capability.
Last edited by mikeflan on Sat Aug 06, 2022 1:10 pm, edited 2 times in total.
I am working a job now, so I won't be responding as fast. If you need a speedy response from me please quote my post. I will try to get to those a little quicker. Thank you.
User avatar
xenopeek
Level 25
Level 25
Posts: 27361
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: [Solved] Create an Empty Zip File Via The Terminal

Post by xenopeek »

7z is p7zip as it said when you ran the command:
$ 7z a -i@/dev/null empty2.zip

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 […]
p7zip is an old port of 7-zip for Unix systems, because 7-Zip at the time didn't do builds for such systems. That 16.02 version is the latest p7zip release, with no updates since 2016.

7-Zip have started to do Linux builds themselves sometime. With the Linux build from 7-Zip themselves you get the latest version, which is from last month. The 7zz command answers:
7-Zip (z) 22.01 (x64) : Copyright (c) 1999-2022 Igor Pavlov : 2022-07-15
You can review the changelog here: https://www.7-zip.org/history.txt. Lots of fixes and improvements since 16.02. I'd rather use the 22.01 version.
Image
Post Reply

Return to “Scripts & Bash”