Archive-Merged

 view release on metacpan or  search on metacpan

lib/Archive/Dir.pm  view on Meta::CPAN

package Archive::Dir;
use strict;
use Carp qw(croak);
use Path::Class;
our $VERSION = '0.02';

=head1 NAME

Archive::Dir - a directory with an API like an Archive::Tar

=head1 SYNOPSIS

    my $ar = Archive::Dir->new('foo');

=head1 METHODS

=cut

sub new {
    my ($class, $directory) = @_;
    my $self = {
        directory => dir($directory),
    };
    bless $self => $class;
    $self
};

sub directory {
    $_[0]->{directory}
};

sub contains_file {
    -f $_[0]->directory->file($_[1])
};

sub get_content {
    my( $self, $file, %options ) = @_;
    $options{ binmode } ||= ':raw';
    $options{ binmode } = "<$options{binmode}";
    $self->directory->file($file)->slurp(iomode => $options{ binmode });
};

sub list_files {
    my ($self,$properties) = @_;
    croak "Listing properties is not (yet) implemented"
        if $properties;
    my @files;
    $self->directory->recurse(callback => sub { push @files, $_[0] if !$_[0]->is_dir});
    map { $_->relative( $self->directory ) } @files
}

sub extract_file {
    my ($self,$file,$target) = @_;
    if ($self->contains_file( $file )) {
        open my $fh, '>', $target
            or croak "Couldn't create '$target': $!";
        binmode $fh;
        print {$fh} $self->get_content($file);
    } else {
        croak "'$file' is not contained in '" . $self->directory . "'";
    };
};

1;

=head1 CAUTION

This module does not implement any encoding/decoding for file names in
the file system. It completely relies on L<Path::Class> to handle this issue.

=head1 REPOSITORY

The public repository of this module is 
L<http://github.com/Corion/archive-merged>.

=head1 SUPPORT

The public support forum of this module is
L<https://perlmonks.org/>.

=head1 BUG TRACKER

Please report bugs in this module via the RT CPAN bug queue at
L<https://rt.cpan.org/Public/Dist/Display.html?Name=Archive-Merged>

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.062 second using v1.00-cache-1.14-grep-28634ff-cpan-ac32402124b )