Merge pull request #2383 from solgenomics/dauglyon-patch-1
[sgn.git] / lib / CXGN / ZipFile.pm
blob2caaf790a6b5032bc0aace7d6c385b3a49ff2f58
2 package CXGN::ZipFile;
4 use Moose;
5 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
6 use SGN::Model::Cvterm;
7 use Data::Dumper;
9 has 'archived_zipfile_path' => (isa => 'Str',
10 is => 'rw',
11 required => 1,
14 has 'extract_directory' => (isa => 'Str',
15 is => 'rw',
18 has 'archived_zip' => (isa => 'Archive::Zip::Archive',
19 is => 'rw',
23 my $archived_zip = Archive::Zip->new();
25 sub BUILD {
26 my $self = shift;
27 unless ( $archived_zip->read( $self->archived_zipfile_path() ) == AZ_OK ) {
28 print STDERR "cannot read given zipfile\n";
29 return;
31 $self->archived_zip($archived_zip);
35 #Assuming that zipfile is a flat list of files.
36 sub file_names {
37 my $self = shift;
38 if (!$self->archived_zip){
39 return;
41 my @file_names = $self->archived_zip()->memberNames();
42 my @file_names_stripped;
43 my @file_names_full;
44 foreach (@file_names) {
45 my @zip_names_split = split(/\//, $_);
46 if ($zip_names_split[1]) {
47 if ($zip_names_split[1] ne '.DS_Store' && $zip_names_split[1] ne '.fieldbook' && $zip_names_split[1] ne '.thumbnails') {
48 my @zip_names_split_ext = split(/\./, $zip_names_split[1]);
49 push @file_names_stripped, $zip_names_split_ext[0];
50 push @file_names_full, $zip_names_split[1];
55 return (\@file_names_stripped, \@file_names_full);
58 sub file_members {
59 my $self = shift;
60 my @ret_members;
61 if (!$self->archived_zip){
62 return;
64 my @file_members = $self->archived_zip()->members();
65 #print STDERR Dumper \@file_members;
66 my %seen_files;
67 foreach (@file_members) {
68 if (exists($seen_files{$_->{'fileName'}}) || $_->{'compressedSize'} == 0 || index($_->{'fileName'}, '.DS_Store') != -1 || index($_->{'fileName'}, '.fieldbook') != -1 || index($_->{'fileName'}, '.thumbnails') != -1) {
69 next;
70 } else {
71 $seen_files{$_->{'fileName'}} = 1;
72 push @ret_members, $_;
75 return \@ret_members;
78 sub extract_files_into_tempdir {
79 my $self = shift;
80 my $temp_dir = shift;