Zip-Player Plays Music Archives:
Part 1

by Daniel Meiß-Wilhelm (Leiche)

One day, a user in a German forum was talking about "zip player." Messing around, I asked him what "zip player" was. He stated that it is a player that can play music from a *.zip archive, it's need for http://www.jamendo.com/, and Foobar can play it.

I searched in our repositories for this function, but I could not find anything. The idea that a player can play music out of an archive was funny, and I wondered if I could make something similar. What I ended up with (and now in the PCLinuxOS repository) is "Zip-Player", a KDE 4 Servicemenu (right click menu) that allows you to play music in *.zip archive files.

pic

So What Do We Need?

The first thing we need is a player that can open directories, then play all files inside. I chose xmms, because it has always been able to play all my music files without any problems. But now it must play music files in a zip-archive? Here a script is required, and I get started writing a simple script file. To create a script, we need to start kwrite and enter:

#!/bin/bash
#

The first line listed in kwrite tells what it is (bash script), and shows the syntax highlighting. We need to save it as 'zip_player.' With the sharp sign "#" you can hide or disable a command. Now we type the command:

xmms $HOME/

But wait a moment, because we must first open an archive. Also, we need unzip. When we open a Konsole and type unzip — help, we get this description:

pic

What we need is unzip <archive> -d <target>.

So we write in our script:

unzip <archive> -d <target> && xmms $HOME/<target>

But we have to wonder if this will not work or not.

  • <archive> must represent the place and name of the archive.
  • <target> is the place where the archive should unpacked.
  • And last but not least, the script must executable.

So how do we make the script file executable? A right mouse click on the script open a dialog. In this dialog, we choose 'Properties' and click on the box that says "Is Executable". Now, when we now click on our script, it'll start in the background, but it doesn't do anything, because unzip has no archive to open.

Kdialog

So exactly what is Kdialog? Open Konsole and type kdialog --help and we get this (in German for me). Doing the same on your computer will display the help text in the language installed on your computer.

pic

With Kdialog, we can create a simple GUI. This will make it easier for us to open an archive for our zip player. So far, our script looks like this:

#!/bin/bash
#
unzip <archive> -d <target> && <1>xmms $HOME/<target>

Now we add the command Kdialog --title "zip-player" --getopenfilename "open…"

The two ampersands tell the next program to wait for the first to complete its job. However, we must set a attribute to give it to unzip. We simply type our command as below:

SAVE=$(kdialog  --title "zip-player" --getopenfilename "open…")

So, we do not need to enter the unzip command, since our script now reads:

#!/bin/bash
#
SAVE=$(kdialog --title "zip-player" --getopenfilename "open…"
unzip $SAVE -d <target> && xmms $HOME/<target>

We now need to tell our script where to store the contents of the archive file:

#!/bin/bash
#
SAVE=$(kdialog --title "zip-player" --getopenfilename "open…"
unzip $SAVE -d $HOME/<folder> && xmms $HOME/<folder>

But why not just save it in the /home folder? When we want to open another archive, we should remove the old folder and files. We can do that with rm -rf. But first, we should create a new folder with mkdir.

Create Remove what else?

Our app will not work, unless we create the new folder. So, our script must take the input *.zip file, extract it to our new folder, and tell xmms to play the music files in our new folder. And, when we must be certain to remove our new folder after xmms is finished playing the files, and nothing more.

#!/bin/bash
#
mkdir $HOME/.zplayer
SAVE=$(kdialog --title "zip-player" --getopenfilename "open…"
unzip $SAVE -d $HOME/.zplayer && xmms $HOME/.zplayer && rm -fr $HOME/.zplayer  | exit
pic

Now we can play our MP3s stored in a zip archive with xmms. Notice: If Zip-Player won't start, then type this in a konsole:

rm -fr $HOME/.zplayer

Later (next month), we will learn how to make a ServiceMenu for Zip-Player in KDE 4. To learn more about the command line, see Peter Kelly's series of articles, "Command Line Interface Intro."