Unzipping Files in Order

 

Over the past couple of years I've enjoyed listening to the audio edition of the Economist newspaper. The material is superb (although I occasionally get the feeling of listening to the Voice of America), the articles are read in a clear voice, the data's encoding is plain MP3, unencumbered by digital rights (restrictions) management silliness, and the audio format is convenient to listen on the metro or while jogging. Unfortunately, the articles in the audio edition's zip file are haphazardly ordered, which, until today, marred the enjoyment of my listening.

Specifically, although the articles are numbered sequentially, they are placed in the zip file in a random order. Here is an example from the September 6th issue:

10 Letters.mp3
13 Briefing - Georgia after the war.mp3
14 Briefing - Islamic finance.mp3
15 Briefing - A short introduction.mp3
11 Briefing - The West and Russia.mp3
12 Briefing - The north Caucasus.mp3
20 United States - Hurricane Gustav.mp3
21 United States - The economy.mp3
22 United States - Swing states_Minnesota.mp3
23 United States - Lexington.mp3
16 United States - The Republican convention.mp3
17 United States - Ron Paul's campaign.mp3
19 United States - Examining Alaska (2).mp3
04 Leaders - British economy.mp3
18 United States - Examining Alaska (1).mp3
25 The Americas - Mexico.mp3
This is a problem, because the files are unzipped in the same order, and my cheap and cheerful MP3 player ends up playing them randomly, as stored.

I solved this through a little shell script that sorts the zip's files and then unzips them in that order. A small complication arises by the fact that both the zip file and the individual files contain spaces in them. Here is the script:

#!/bin/sh
# Obtain list of the zip contents
unzip -Z1 "$1"  |
# Order files numerically
sort -n |
# Run unzip on each newline-terminated file
xargs -d \\n -n 1 unzip "$1"
The script takes as its single argument a zip file, and orderly unzips the file's contents in the current directory. Using a method I've outlined earlier one can even run the script under Cygwin and Windows.

Comments   Toot! Share


Last modified: Thursday, September 11, 2008 6:05 pm

Creative Commons Licence BY NC

Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.