Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Creating files within a .zip

by wumpus42 (Novice)
on Sep 28, 2012 at 15:08 UTC ( [id://996240]=perlquestion: print w/replies, xml ) Need Help??

wumpus42 has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to create a file, that does not exist on the filesystem, within a .zip file and write to it?

  • foo/bar.txt does not exist on the filesystem
  • I'd like to create an empty foo/bar.txt file within the foo.zip file and write to it.

I guess I'm looking for a way to treat a .zip file as a filesystem.

I've checked out information pertaining to Archive::Zip and IO::Compress::Zip but it's not jumping out at me.

Replies are listed 'Best First'.
Re: Creating files within a .zip
by pmqs (Friar) on Sep 28, 2012 at 15:41 UTC
    Try this.
    use strict; use warnings; use IO::Compress::Zip qw(:all); { my $zip = new IO::Compress::Zip "/tmp/my.zip", Name => "foo/bar.txt" or die "Cannot create zip file : $ZipError\n" ; print $zip "hello world" ; } # Check the zip file was created system "unzip -l /tmp/my.zip" ;
    Here is what I get this when I run that code.
    Archive: /tmp/my.zip Length Date Time Name --------- ---------- ----- ---- 11 09-28-2012 16:44 foo/bar.txt --------- ------- 11 1 file
Re: Creating files within a .zip
by wumpus42 (Novice) on Sep 28, 2012 at 16:22 UTC

    This works with perl5 (revision 5 version 16 subversion 1):

    #!/opt/perl/bin/perl use warnings; use strict; use diagnostics; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); MAIN: { my $zd = 'foobar'; my $zip = Archive::Zip->new(); my $d = $zip->addDirectory( $zd ); my $f = $zip->addString( 'this is a test', "$zd/foo.txt" ); $f->desiredCompressionMethod( COMPRESSION_DEFLATED ); $f = $zip->addString( 'of the emergency broadcast system', "$zd/ba +r.txt" ); $f->desiredCompressionMethod( COMPRESSION_DEFLATED ); unless ( $zip->writeToFileNamed("$zd.zip") == AZ_OK ) { die 'write error'; } exit 0; }
    $ unzip -l foobar.zip
    Archive:  foobar.zip
      Length     Date   Time    Name
     --------    ----   ----    ----
            0  09-28-12 11:18   foobar/
           14  09-28-12 11:18   foobar/foo.txt
           33  09-28-12 11:18   foobar/bar.txt
     --------                   -------
           47                   3 files
    $
    

      In the above example/solution the foobar/ directory never existed in the filesystesm. It only exists in the .zip file.

        Has Archive::Zip given you what you want then?
Re: Creating files within a .zip
by MidLifeXis (Monsignor) on Sep 28, 2012 at 16:21 UTC

    IO::Compress::Zip can read from a \$file_contents variable for its input (see the zip function). Is that what you are looking for?

    --MidLifeXis

Re: Creating files within a .zip
by kennethk (Abbot) on Sep 28, 2012 at 15:19 UTC
    Is this perhaps an XY Problem? Why can't you create your file foo/bar.txt and then create/add it to the archive?

    To my (possibly flawed) understanding, you are correct that you cannot open a writable file inside a zip archive. This should make sense, given that compression algorithms in general need to analyze a complete file in order to determine how to compress the file effectively.

    Perhaps File::Temp's tempdir would be helpful here?


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I can certainly create the file(s) first, and I've been doing that. But I've got many hundreds of files to put in the .zip file, each one would have to be created on the filesystem then a string buffer written to each first before putting them into a .zip file? I was looking for a way to skip the middle-man step (creating the foo/bar.txt file on the filesystem).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://996240]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found