LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to Add files to a Zip archive with out unzipping it !! (https://www.linuxquestions.org/questions/programming-9/script-to-add-files-to-a-zip-archive-with-out-unzipping-it-593890/)

prashix 10-23-2007 06:44 AM

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.

matthewg42 10-23-2007 06:49 AM

Quote:

Originally Posted by prashix (Post 2933561)
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

prashix 10-23-2007 06:55 AM

Quote:

Originally Posted by matthewg42 (Post 2933564)
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?

matthewg42 10-23-2007 07:08 AM

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.

prashix 10-23-2007 07:19 AM

Quote:

Originally Posted by matthewg42 (Post 2933590)
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.

prashix 10-23-2007 07:20 AM

Quote:

Originally Posted by matthewg42 (Post 2933590)
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.

prashix 10-23-2007 07:20 AM

Quote:

Originally Posted by matthewg42 (Post 2933590)
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.

matthewg42 10-23-2007 07:23 AM

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"


prashix 10-23-2007 07:32 AM

Quote:

Originally Posted by matthewg42 (Post 2933613)
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??

matthewg42 10-23-2007 07:34 AM

Are you going to add a new condition each time I reply? How about you explain the exact situation first?

prashix 10-23-2007 07:59 AM

Quote:

Originally Posted by matthewg42 (Post 2933624)
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 ?

matthewg42 10-23-2007 09:00 AM

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.

prashix 10-23-2007 09:19 AM

Everything seems fine !! I develop the script soon and let u know the result..

Thanks for the solution Matthew..


All times are GMT -5. The time now is 02:49 AM.