Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl use warnings; use strict; use Archive::Zip qw( :ERROR_CODES ); my $File = shift or die "Must supply a file name.\n"; Archive::Zip::setErrorHandler sub { die @_ }; # Make errors fatal my $zip = Archive::Zip->new(); $zip->read($File); my $flag; # Flag if there is more than one "root item", be it file or subdir my $first = ($zip->members())[0]->fileName() =~ m{^([^/]*)/} && $1; for ($zip->members()) { if( # Flag if it's not under a subdir... $_->fileName() !~ m{/} or $_->fileName() =~ m{^\./[^/]+$} or # ...or if it's under a different subdir ($_->fileName() =~ m{^([^/]*)/} and $1 ne $first) ) { $flag = 1; last; } } # Alternatively, you could just... # Flag if any files are not under a directory # (you could still have multiple subdirs extracted) #for ($zip->members()) { # if($_->fileName() !~ m{/} or $_->fileName() =~ m{^\./[^/]+$}) { # $flag = 1; # last; # } #} if($flag) { (my $folder = $File) =~ s/\.zip$//; # Relocate all members into a subdir who's name is based on the zip +file for my $member ($zip->members()) { $member->fileName($folder . '/' . $member->fileName()); } } # Now that we know it's safe, go ahead and unpack it # Normally, would just use $zip->extractTree() but there seems to # be a bug that adds a single . to the begining of all the top-level # files. This, of course, makes them hidden (by default) under *nix. $zip->extractMember($_) for $zip->members; # Or, of course, you could just $zip->overwrite() to save it back.

In reply to Ensure Zip files always unpack to a single subdirectory by bbfu

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-19 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found