Skip to content

How to use the OO interface? #9

Open

Description

@XSven

Although I have read the documentation of IO::Compress::Gzip, it is unclear to me how the following functional call

gzip($inputFile => $outputFilehandle, {Strict => 0}) or die "gzip failed: $GzipError\n";

could be translated into an OO approach. $inputFile is a filename. $outputFilehandle is a raw read+write filehandle.

Furthermore I would like to know which instance(object) method I could use to access the header information of the zip archive. I have seen IO::Uncompress::Gunzip::getHeaderInfo() but I cannot believe that no such method exists for a gzip object itself because such an object should known how it has created the zip archive including its header.

Activity

  1. pmqs commented on Feb 13, 2020

    @pmqs
    Owner

    Although I have read the documentation of IO::Compress::Gzip, it is unclear to me how the following functional call

    gzip($inputFile => $outputFilehandle, {Strict => 0}) or die "gzip failed: $GzipError\n";

    could be translated into an OO approach. $inputFile is a filename. $outputFilehandle is a raw read+write filehandle.

    here is the equivalent OO

    use IO::Compress::Gzip qw($GzipError) ;
    
    my $inputFile = "/tmp/x";
    
    my $gz = new IO::Compress::Gzip $outputFilehandle, Strict =>0
        or die "Cannot create gzip object: $GzipError\n";
    
    open my $fh, '<', $inputFile
        or die "Cannot open $inputFile: $!\n" ;
    
    while (read($fh, my $buffer, 1024 * 16))
    {
        $gz->write($buffer);
    }
    $gz->close();
    
    

    Furthermore I would like to know which instance(object) method I could use to access the header information of the zip archive. I have seen IO::Uncompress::Gunzip::getHeaderInfo() but I cannot believe that no such method exists for a gzip object itself because such an object should known how it has created the zip archive including its header.

    There is no public interface to access the gzip header in IO::Compress::Gzip because I've never seen the need. Can you explain the use-case you want it for?

  2. XSven commented on Feb 13, 2020

    @XSven
    Author

    To be honest, the OO equivalent disappoints me. I would have expected somethink like

    $gz->compress($inputFile);

    that means an instance(object) method that has one argument, that is the input file.

    We could consider this

    How do i tell in Perl what the size of a file inside a gzip archive is without unpacking the whole file?

    or the question: What is the equivalent of gzip --list as a use-case? But our use-case that has brought us to the above stackoverflow article was this:

    We have used the functional interface of IO::Compress::Gzip to zip an input file. Due to a race condition (a bug at our end:-() there was a discrepancy between the content of the original input file and the zip file. The zip file was not(!) corrupt but after unzipping the zip file it was different from original input file due to the race condition. We thought it would be a good idea to log the compressed and the uncompressed file size (ISIZE) from the perspective of IO::Compress::Gzip. We don't wanted to make extra stat() calls before and/or after the crompress action. We wanted the compress action(object) to provide a feedback.

    Thx

  3. self-assigned this
    on Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions