Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Creating a tarball [SOLUTION]

by kcott (Bishop)
on Feb 07, 2019 at 23:27 UTC ( #1229575=note: print w/replies, xml ) Need Help??


in reply to Creating a tarball

My thanks to all who provided help and assistance with this. I now have a working test solution which I present below as this may be useful for others.

I used Path::Tiny, as originally shown by ++tybalt89, to generate the list of files for archiving. I needed to tweak the suggested code in a few places, as follows:

  • Path::Tiny's visit() method "Executes a callback for each child of a directory.". This meant that the parent directory wasn't added to the array for add_files(): easily fixed by preloading that array with the parent directory.
  • For my purposes, the contents of the directory structure was well known, so no filtering — based on file types, permissions or other characteristics — was required. In other situations, some filtering may be required: see ++tybalt89's and ++Tux' examples[1,2] for possible ways to achieve this.
  • I also needed to change directories to get the exact archive I wanted. This may not be necessary in other circumstances. Note the use of the autodie pragma in a limited, lexical scope to handle problems with chdir and provide useful feedback if they occur.
  • Not an actual tweak required by this code, but use 5.016; was added because it mirrors the version I'm coding to for $work$ (and increases confidence that this won't have issues in my production environment). If you're using this code as a template, and not adding anything fancy, replacing that with a simple use strict; would probably be fine.

Here's the test script. Note that the directory structure under $src_dir is exactly the same as that presented in the OP.

#!/usr/bin/env perl use 5.016; use warnings; use Archive::Tar; use Cwd; use Path::Tiny; my $src_dir = '/Users/ken/tmp/test_arch/src'; my $zip_dir = '/Users/ken/tmp/test_arch/zip'; my $top_dir = 'fred'; my $zip_name = 'fred.tar.gz'; my $zip_path = path($zip_dir, $zip_name); { use autodie; my $cur_dir = getcwd; chdir $src_dir or die; my @tar_files = ($top_dir); path($top_dir)->visit( sub { push @tar_files, "$_" }, { recurse => 1 } ); my $tar = Archive::Tar::->new(); $tar->add_files(@tar_files); $tar->write($zip_path, COMPRESS_GZIP); chdir $cur_dir or die; }

A sample run, as well as various checks, are in the spoiler:

ken@ganymede: ~/tmp/test_arch/zip $ ls -al total 0 drwxr-xr-x 2 ken staff 68 Feb 8 09:55 . drwxr-xr-x 5 ken staff 170 Feb 8 07:59 .. ken@ganymede: ~/tmp/test_arch/zip $ ../bin/arch_fred.pl ken@ganymede: ~/tmp/test_arch/zip $ ls -al total 4 drwxr-xr-x 3 ken staff 102 Feb 8 09:55 . drwxr-xr-x 5 ken staff 170 Feb 8 07:59 .. -rw-r--r-- 1 ken staff 209 Feb 8 09:55 fred.tar.gz ken@ganymede: ~/tmp/test_arch/zip $ tar zxvf fred.tar.gz > fred.INSTALL 2>&1 ken@ganymede: ~/tmp/test_arch/zip $ ls -al total 8 drwxr-xr-x 5 ken staff 170 Feb 8 09:56 . drwxr-xr-x 5 ken staff 170 Feb 8 07:59 .. drwxr-xr-x 6 ken staff 204 Feb 8 07:59 fred -rw-r--r-- 1 ken staff 104 Feb 8 09:56 fred.INSTALL -rw-r--r-- 1 ken staff 209 Feb 8 09:55 fred.tar.gz ken@ganymede: ~/tmp/test_arch/zip $ cat fred.INSTALL x fred x fred/asd x fred/derf x fred/qwe x fred/zxc x fred/derf/fgh x fred/derf/rty x fred/derf/fgh/vbn ken@ganymede: ~/tmp/test_arch/zip $ ls -alR fred fred: total 0 drwxr-xr-x 6 ken staff 204 Feb 8 07:59 . drwxr-xr-x 5 ken staff 170 Feb 8 09:56 .. -rw-r--r-- 1 ken staff 0 Feb 8 07:59 asd drwxr-xr-x 4 ken staff 136 Feb 8 07:59 derf -rw-r--r-- 1 ken staff 0 Feb 8 07:59 qwe -rw-r--r-- 1 ken staff 0 Feb 8 07:59 zxc fred/derf: total 0 drwxr-xr-x 4 ken staff 136 Feb 8 07:59 . drwxr-xr-x 6 ken staff 204 Feb 8 07:59 .. drwxr-xr-x 3 ken staff 102 Feb 8 07:59 fgh -rw-r--r-- 1 ken staff 0 Feb 8 07:59 rty fred/derf/fgh: total 0 drwxr-xr-x 3 ken staff 102 Feb 8 07:59 . drwxr-xr-x 4 ken staff 136 Feb 8 07:59 .. -rw-r--r-- 1 ken staff 0 Feb 8 07:59 vbn ken@ganymede: ~/tmp/test_arch/zip $ cd ../.. ken@ganymede: ~/tmp $ ls -alR fred # <=== Original files referenced in the OP fred: total 0 drwxr-xr-x 6 ken staff 204 Feb 7 11:17 . drwxr-xr-x 46 ken staff 1564 Feb 8 07:59 .. -rw-r--r-- 1 ken staff 0 Feb 7 11:17 asd drwxr-xr-x 4 ken staff 136 Feb 7 11:17 derf -rw-r--r-- 1 ken staff 0 Feb 7 11:17 qwe -rw-r--r-- 1 ken staff 0 Feb 7 11:17 zxc fred/derf: total 0 drwxr-xr-x 4 ken staff 136 Feb 7 11:17 . drwxr-xr-x 6 ken staff 204 Feb 7 11:17 .. drwxr-xr-x 3 ken staff 102 Feb 7 11:18 fgh -rw-r--r-- 1 ken staff 0 Feb 7 11:17 rty fred/derf/fgh: total 0 drwxr-xr-x 3 ken staff 102 Feb 7 11:18 . drwxr-xr-x 4 ken staff 136 Feb 7 11:17 .. -rw-r--r-- 1 ken staff 0 Feb 7 11:18 vbn ken@ganymede: ~/tmp $

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229575]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-05-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (72 votes). Check out past polls.

    Notices?