• mkzip and a single file

    From greg@21:1/5 to All on Sun Jun 2 06:46:44 2024
    Hello,

    I'm using mkzip from tcllib to create ZIP archives. https://core.tcl-lang.org/tcllib/doc/trunk/embedded/md/tcllib/files/modules/zip/mkzip.md

    Problem:

    A directory that contains multiple files.
    I only want to zip a specific file from that directory,
    without including the entire absolute path or other files in the
    directory in the ZIP archive.

    Target:

    only testfile01.txt in the zip file

    1.)
    set zipfile [file join $sourcedir "testfile01.zip"]
    set options [list [file join $sourcedir testfile01.txt]] :zipfile::mkzip::mkzip $zipfile {*}$options

    works, but with absolute path

    2.)
    set zipfile [file join $sourcedir "testfile01.zip"]
    set options [list -directory [file join $sourcedir] -exclude {d* *02.txt}] ::zipfile::mkzip::mkzip $zipfile {*}$options

    works without absolute path, but -exclude has to be adjusted
    individually each time

    Is there a direct way?


    best regards
    Gregor

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From greg@21:1/5 to All on Sun Jun 2 10:52:22 2024
    Am 02.06.24 um 06:46 schrieb greg:
    Hello,

    I'm using mkzip from tcllib to create ZIP archives. https://core.tcl-lang.org/tcllib/doc/trunk/embedded/md/tcllib/files/modules/zip/mkzip.md

    Problem:

    A directory that contains multiple files.
    I only want to zip a specific file from that directory,
    without including the entire absolute path or other files in the
    directory in the ZIP archive.

    Target:

    only testfile01.txt in the zip file

    1.)
    set zipfile [file join $sourcedir "testfile01.zip"]
    set options [list  [file join $sourcedir testfile01.txt]] :zipfile::mkzip::mkzip $zipfile {*}$options

    works, but with absolute path

    2.)
    set zipfile [file join $sourcedir "testfile01.zip"]
    set options [list -directory [file join $sourcedir] -exclude {d* *02.txt}] ::zipfile::mkzip::mkzip $zipfile {*}$options

    works without absolute path, but -exclude has to be adjusted
    individually each time

    Is there a direct way?


    best regards
    Gregor

    Have a solution;

    3.)

    set zipfile [file join $sourcedir "testfile01.zip"]
    set file_to_zip [file join $sourcedir testfile01.txt]
    set tempdir [fileutil::tempdir mkzip]
    set temp_file [file join $tempdir testfile01.txt]
    file copy -force $file_to_zip $temp_file
    set options [list -directory [file join $tempdir] testfile01.txt] ::zipfile::mkzip::mkzip $zipfile {*}$options
    file delete -force $tempdir

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From clt.to.davebr@dfgh.net@21:1/5 to All on Sun Jun 2 14:29:22 2024
    From: greg <gregor.ebbing@gmx.de>
    Date: Sun, 2 Jun 2024 10:52:22 +0200
    Subject: Re: mkzip and a single file

    This seems to work for me (on Slackware 15ish)

    cd $sourcedir
    zipfile::mkzip::mkzip testfile01.zip testfile01.txt

    Save and restore the current directory using pwd, cd and try if desired.


    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From greg@21:1/5 to All on Sun Jun 2 19:23:33 2024
    Am 02.06.24 um 16:29 schrieb clt.to.davebr@dfgh.net:
    From: greg <gregor.ebbing@gmx.de>
    Date: Sun, 2 Jun 2024 10:52:22 +0200
    Subject: Re: mkzip and a single file

    This seems to work for me (on Slackware 15ish)

    cd $sourcedir
    zipfile::mkzip::mkzip testfile01.zip testfile01.txt

    Save and restore the current directory using pwd, cd and try if desired.


    Dave B

    Hello Dave,

    Thanks for the solution!.
    (for me the best solution)

    4.
    set savedir [pwd]
    cd [file join $sourcedir tmpzip]
    set options [list testfile01.txt]
    ::zipfile::mkzip::mkzip $zipfile {*}$options
    cd $savedir



    A mistake from me in 3
    Correct:
    set tempdir [::fileutil::maketempdir]


    Gregor

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From clt.to.davebr@dfgh.net@21:1/5 to All on Mon Jun 3 15:37:50 2024
    Consider wrapping mkzip in a try/finally so errors do not leave you in an unexpected directory:

    set savedir [pwd]
    cd [file join $sourcedir tmpzip]
    try {
    set options [list testfile01.txt]
    ::zipfile::mkzip::mkzip $zipfile {*}$options
    } finally {cd $savedir}



    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)