Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This program reads a list of files from the directory specified on the command line, creates an array of hashes, where each file has key path, size, and sha256sum.

I am trying to create a gzipped tarball of the files, where each files is name the checksum appended with the file's original extension. I create a gzipped tarball of the files successfully. However, when I try to use Archive::Tar's rename method, I am met with this error: No such file in archive: '/path/to/file1.txt' at ./program.pl line 62. This error repeats for each file in the archive.

Is it because the archive is just a flat list of files? If so, how does one use the rename method?

use strict; use warnings; use Data::Dumper qw(Dumper); use File::Spec qw(catfile rel2abs); use Digest::SHA qw(sha256_hex); use Archive::Tar; use Archive::Tar::File; my $dir = $ARGV[0]; my $url = $ARGV[1]; my @AoH; my @checksumfiles; my $tar = Archive::Tar->new; my $archive = "archive.tar.gz"; opendir DIR, $dir or die "cannot open dir $dir: $!\n"; chdir $dir or die "cannot navigate to dir $dir: $!\n"; while(my $file = readdir DIR) { next unless(-f File::Spec->catfile($dir, $file)); next if($file =~ m/^\./); my $fullpath = File::Spec->rel2abs($file); my $fullsize = -s File::Spec->catfile($dir, $file); my $fullid = sha256_hex($fullpath); my %hash = ( path => $fullpath, size => $fullsize, id => $fullid, ); push(@AoH, \%hash); } my @array; for my $i(0..$#AoH) { no warnings 'uninitialized'; my ($ext) = $AoH[$i]{path} =~ (/(\.[^.]+)$/); my $idext = $AoH[$i]{id} . $ext; push(@checksumfiles, $idext); push(@array, $AoH[$i]{path}); } Archive::Tar->create_archive($archive, COMPRESS_GZIP, @array); #print Archive::Tar->list_archive($archive, COMPRESS_GZIP), "\n"; for my $i(0..$#array) { $tar->rename($array[$i], $checksumfiles[$i]); } print Dumper sort \@array; print Dumper sort \@checksumfiles; #print Dumper sort \@AoH;

In reply to Rename files in gzip tarball: No such file in archive: '/path/to/file1.txt' by Bowlslaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2021-04-13 18:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?