LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-23-2007, 06:44 AM   #1
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Rep: Reputation: 0
Script to Add files to a Zip archive with out unzipping it !!


Hii

I need to add files to an already existing Zip archive with out unzipping it. Without unzipping it because the Zip archive is very large for which space won't be enough to contain both the archive and actual files. but where as files to be added are very small.

I am thinking of a script to do this automatically when ever files are ready to be added.

Does this command work ?

$> zip -g ....

Its something like adding patches to an already existing module.
 
Old 10-23-2007, 06:49 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by prashix View Post
Does this command work ?

$> zip -g ....

Its something like adding patches to an already existing module.
Works for me, e.g.
Code:
zip -g mylargezipfile.zip filetoadd.txt
 
Old 10-23-2007, 06:55 AM   #3
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
Works for me, e.g.
Code:
zip -g mylargezipfile.zip filetoadd.txt
zip -g mylargezipfile.zip filetoadd.txt

good .. but the point is.. its something like this

mylargezipfile.zip contains a folder say

Module
|-------Folder one
|----Folder two

Now say, i have to add "filetoadd.txt" into Folder two...

Sometimes we may need to add a folder itself to the zip archive..
Then?
 
Old 10-23-2007, 07:08 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Then create your file to add in a folder of the proper name, and add it. If your folder names contain spaces, don't forget to quote them.
 
Old 10-23-2007, 07:19 AM   #5
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
Then create your file to add in a folder of the proper name, and add it.
how to do that with the command.. i mean EDIT this..

zip -g mylargezipfile.zip filetoadd.txt


see

In "mylargezipfile.zip"
the structure is like this.
Module
...|------- Folder1
.......|---- Folder2
................|-no file.


After running the script it should be like this in the zip itself

Module
|----- Folder1
...|-----Folder2
............|---- filetoadd.txt


This should be the structure inside the Zip file.
 
Old 10-23-2007, 07:20 AM   #6
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
Then create your file to add in a folder of the proper name, and add it.
F-o-f Thanks !!

But, how to do that with the command.. i mean EDIT this..

zip -g mylargezipfile.zip filetoadd.txt


see

In "mylargezipfile.zip"
the structure is like this.
Module
...|------- Folder1
.......|---- Folder2
................|-no file.


After running the script it should be like this in the zip itself

Module
|----- Folder1
...|-----Folder2
............|---- filetoadd.txt


This should be the structure inside the Zip file.
 
Old 10-23-2007, 07:20 AM   #7
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
Then create your file to add in a folder of the proper name, and add it.
F-o-f Thanks !!

But, how to do that with the command.. i mean EDIT this..

zip -g mylargezipfile.zip filetoadd.txt


see

In "mylargezipfile.zip"
the structure is like this.
Module
...|------- Folder1
.......|---- Folder2
................|-no file.


After running the script it should be like this in the zip itself

Module
|----- Folder1
...|-----Folder2
............|---- filetoadd.txt


This should be the structure inside the Zip file.

I hope you got my point.
 
Old 10-23-2007, 07:23 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Is there an echo in here?

ECHO Echo echo e... ....

So there is.

But back to the question. Make a directory and put the file in there, and then make the command referencing the path to the file (we call them directories on unix-like OSes - "folder" is a word from the Windows world).

i.e.
Code:
mkdir "folder one"
cp filetoadd.txt "folder one"
zip mylargezipfile.zip "folder one/filetoadd.txt"
rm -rf "folder one"
 
Old 10-23-2007, 07:32 AM   #9
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
Is there an echo in here?

ECHO Echo echo e... ....

So there is.

But back to the question. Make a directory and put the file in there, and then make the command referencing the path to the file (we call them directories on unix-like OSes - "folder" is a word from the Windows world).

i.e.
Code:
mkdir "folder one"
cp filetoadd.txt "folder one"
zip mylargezipfile.zip "folder one/filetoadd.txt"
rm -rf "folder one"
Sry for that... problem with the browser..

Anyway what happens if folder1 already contains some files in it and i have to add this new file [say, new patch] to it..

I didnt get, why we are creating a directory again ?? and add this file.
Directory exists already inside the zip, right??
 
Old 10-23-2007, 07:34 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Are you going to add a new condition each time I reply? How about you explain the exact situation first?
 
Old 10-23-2007, 07:59 AM   #11
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
Are you going to add a new condition each time I reply? How about you explain the exact situation first?
Heh heh !! Sry abt that matthew.,. i dont mean to do that.

Ok here is the situation...

All modules in my project are stored as zip files.. there are many such modules..

Now each and every module will be updated based on user requirements... so we have to develop a patch to it and add to those zip files.. Since zip files are big we cant unzip it everytime.

And each module contains some hundreds of directories and sub directories and files in them. Now, based on the requirement I have to update that particular sub-directory with the patch.

Directories can be like this.

ModuleXX
...|---DBConn
...|---SourceFiles
..........|--GUI.c
..........|--Random.c
...|---Patches


Something like that..
Everytime a new patch is developed it has to be automatically updated i.e added to say "patches" or "DBConn" directory.

Whats ur idea on this ?? how to do ?
 
Old 10-23-2007, 09:00 AM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You can create a temporary directory, change into it, create the sub-directory you want with the new file and then add it. You can specify the .zip file by its full path, so you don't have to move it or anything like that.

For example. Say your .zip file is: /home/matthew/zipfiles/mybigfile.zip and you want to add /home/matthew/patches/mynewpatch to the patches sub-directory inside mybigfile.zip, you might write a little script which takes the full path of the .zip file and the patch file and the sub-directory name:
Code:
#!/bin/bash

main () {
        # check we have the correct number of paramaters
        if [ $# -lt 2 ] || [ $# -gt 3 ]; then
                usage 1
        fi

        # check args 1 and 2 are full paths
        case "$1" in
        /*)
                echo "zipfile ok: $1"
                ;;
        *)
                echo "zipfile should be full path"
                exit 2
        esac

        case "$2" in
        /*)
                echo "addfile ok: $2"
                ;;
        *)
                echo "addfile should be full path"
                exit 2
        esac

        case "$3" in
        /*)
                echo "addpath should be relative only, and not starting with . or .."
                exit 2
                ;;
        .*)
                echo "addpath should be relative only, and not starting with . or .."
                exit 2
                ;;
        *)
                echo "addpath ok: $3"
                ;;
        esac

        tmpdir=$(mktemp -d)
        cd "$tmpdir"
        if [ "$3" != "" ]; then
                mkdir -p "$3"
                cp "$2" "$3"
                add="$3/${2##*/}"
        else
                cp "$2" .
                add=${2##*/}
        fi

        zip -g "$1" "$add"
        cd /
        rm -rf "$tmpdir"
}

usage () {
        # print out a quick help message and exit
        cat <<EOD
Usage:
  $THISSCRIPT zipfile addfile [addpath]

You must use the full path for zipfile and addfile.  The addfile
will be added into the zipfile in path addpath.  e.g.

  $THISSCRIPT /path/to/file.zip /path/to/addfile newfiles

...will add newfiles/addfile inside file.zip

If addpath is empty, addfile will be added into the root of zipfile.

EOD

        # exit using parameter 1 as value else 0.
        exit ${1:-0}
}

# set the basename of the script for the usage message
THISSCRIPT=${0##*/}

# call the main function
main "$@"
It looks like a lot of code, but most of it is just checking the parameters make sense.
 
Old 10-23-2007, 09:19 AM   #13
prashix
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Everything seems fine !! I develop the script soon and let u know the result..

Thanks for the solution Matthew..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unzipping .zip files in linux dr_zayus69 Linux - Software 5 09-27-2004 06:47 PM
Unzipping a .zip file sduffy89 Linux - Software 3 08-25-2004 08:55 AM
shell script - add file to archive ? xconspirisist Linux - Software 2 03-20-2004 03:27 PM
What Are archive.zip files? kajensen Linux - General 1 09-21-2003 04:00 AM
unzipping .zip files...how?? Robert0380 Linux - General 2 04-11-2002 04:54 AM


All times are GMT -5. The time now is 08:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration