{
    "mode": "perldoc",
    "parameter": "IO::Compress::Bzip2",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/IO%3A%3ACompress%3A%3ABzip2/json",
    "generated": "2026-06-10T17:30:54Z",
    "synopsis": "use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\nmy $status = bzip2 $input => $output [,OPTS]\nor die \"bzip2 failed: $Bzip2Error\\n\";\nmy $z = IO::Compress::Bzip2->new( $output [,OPTS] )\nor die \"bzip2 failed: $Bzip2Error\\n\";\n$z->print($string);\n$z->printf($format, $string);\n$z->write($string);\n$z->syswrite($string [, $length, $offset]);\n$z->flush();\n$z->tell();\n$z->eof();\n$z->seek($position, $whence);\n$z->binmode();\n$z->fileno();\n$z->opened();\n$z->autoflush();\n$z->inputlinenumber();\n$z->newStream( [OPTS] );\n$z->close() ;\n$Bzip2Error ;\n# IO::File mode\nprint $z $string;\nprintf $z $format, $string;\ntell $z\neof $z\nseek $z, $position, $whence\nbinmode $z\nfileno $z\nclose $z ;",
    "sections": {
        "NAME": {
            "content": "IO::Compress::Bzip2 - Write bzip2 files/buffers\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\n\nmy $status = bzip2 $input => $output [,OPTS]\nor die \"bzip2 failed: $Bzip2Error\\n\";\n\nmy $z = IO::Compress::Bzip2->new( $output [,OPTS] )\nor die \"bzip2 failed: $Bzip2Error\\n\";\n\n$z->print($string);\n$z->printf($format, $string);\n$z->write($string);\n$z->syswrite($string [, $length, $offset]);\n$z->flush();\n$z->tell();\n$z->eof();\n$z->seek($position, $whence);\n$z->binmode();\n$z->fileno();\n$z->opened();\n$z->autoflush();\n$z->inputlinenumber();\n$z->newStream( [OPTS] );\n\n$z->close() ;\n\n$Bzip2Error ;\n\n# IO::File mode\n\nprint $z $string;\nprintf $z $format, $string;\ntell $z\neof $z\nseek $z, $position, $whence\nbinmode $z\nfileno $z\nclose $z ;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides a Perl interface that allows writing bzip2 compressed data to files or\nbuffer.\n\nFor reading bzip2 files/buffers, see the companion module IO::Uncompress::Bunzip2.\n",
            "subsections": []
        },
        "Functional Interface": {
            "content": "A top-level function, \"bzip2\", is provided to carry out \"one-shot\" compression between buffers\nand/or files. For finer control over the compression process, see the \"OO Interface\" section.\n\nuse IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\n\nbzip2 $inputfilenameorreference => $outputfilenameorreference [,OPTS]\nor die \"bzip2 failed: $Bzip2Error\\n\";\n\nThe functional interface needs Perl5.005 or better.\n\nbzip2 $inputfilenameorreference => $outputfilenameorreference [, OPTS]\n\"bzip2\" expects at least two parameters, $inputfilenameorreference and\n$outputfilenameorreference and zero or more optional parameters (see \"Optional Parameters\")\n\nThe $inputfilenameorreference parameter\nThe parameter, $inputfilenameorreference, is used to define the source of the uncompressed\ndata.\n\nIt can take one of the following forms:\n\nA filename\nIf the $inputfilenameorreference parameter is a simple scalar, it is assumed to be a\nfilename. This file will be opened for reading and the input data will be read from it.\n\nA filehandle\nIf the $inputfilenameorreference parameter is a filehandle, the input data will be read\nfrom it. The string '-' can be used as an alias for standard input.\n\nA scalar reference\nIf $inputfilenameorreference is a scalar reference, the input data will be read from\n$$inputfilenameorreference.\n\nAn array reference\nIf $inputfilenameorreference is an array reference, each element in the array must be a\nfilename.\n\nThe input data will be read from each file in turn.\n\nThe complete array will be walked to ensure that it only contains valid filenames before\nany data is compressed.\n\nAn Input FileGlob string\nIf $inputfilenameorreference is a string that is delimited by the characters \"<\" and \">\"\n\"bzip2\" will assume that it is an *input fileglob string*. The input is the list of files\nthat match the fileglob.\n\nSee File::GlobMapper for more details.\n\nIf the $inputfilenameorreference parameter is any other type, \"undef\" will be returned.\n\nThe $outputfilenameorreference parameter\nThe parameter $outputfilenameorreference is used to control the destination of the compressed\ndata. This parameter can take one of these forms.\n\nA filename\nIf the $outputfilenameorreference parameter is a simple scalar, it is assumed to be a\nfilename. This file will be opened for writing and the compressed data will be written to\nit.\n\nA filehandle\nIf the $outputfilenameorreference parameter is a filehandle, the compressed data will be\nwritten to it. The string '-' can be used as an alias for standard output.\n\nA scalar reference\nIf $outputfilenameorreference is a scalar reference, the compressed data will be stored\nin $$outputfilenameorreference.\n\nAn Array Reference\nIf $outputfilenameorreference is an array reference, the compressed data will be pushed\nonto the array.\n\nAn Output FileGlob\nIf $outputfilenameorreference is a string that is delimited by the characters \"<\" and\n\">\" \"bzip2\" will assume that it is an *output fileglob string*. The output is the list of\nfiles that match the fileglob.\n\nWhen $outputfilenameorreference is an fileglob string, $inputfilenameorreference must\nalso be a fileglob string. Anything else is an error.\n\nSee File::GlobMapper for more details.\n\nIf the $outputfilenameorreference parameter is any other type, \"undef\" will be returned.\n",
            "subsections": [
                {
                    "name": "Notes",
                    "content": "When $inputfilenameorreference maps to multiple files/buffers and\n$outputfilenameorreference is a single file/buffer the input files/buffers will be stored in\n$outputfilenameorreference as a concatenated series of compressed data streams.\n"
                },
                {
                    "name": "Optional Parameters",
                    "content": "The optional parameters for the one-shot function \"bzip2\" are (for the most part) identical to\nthose used with the OO interface defined in the \"Constructor Options\" section. The exceptions\nare listed below\n\n\"AutoClose => 0|1\"\nThis option applies to any input or output data streams to \"bzip2\" that are filehandles.\n\nIf \"AutoClose\" is specified, and the value is true, it will result in all input and/or\noutput filehandles being closed once \"bzip2\" has completed.\n\nThis parameter defaults to 0.\n\n\"BinModeIn => 0|1\"\nThis option is now a no-op. All files will be read in binmode.\n\n\"Append => 0|1\"\nThe behaviour of this option is dependent on the type of output data stream.\n\n*    A Buffer\n\nIf \"Append\" is enabled, all compressed data will be append to the end of the output\nbuffer. Otherwise the output buffer will be cleared before any compressed data is\nwritten to it.\n\n*    A Filename\n\nIf \"Append\" is enabled, the file will be opened in append mode. Otherwise the contents\nof the file, if any, will be truncated before any compressed data is written to it.\n\n*    A Filehandle\n\nIf \"Append\" is enabled, the filehandle will be positioned to the end of the file via a\ncall to \"seek\" before any compressed data is written to it. Otherwise the file pointer\nwill not be moved.\n\nWhen \"Append\" is specified, and set to true, it will *append* all compressed data to the\noutput data stream.\n\nSo when the output is a filehandle it will carry out a seek to the eof before writing any\ncompressed data. If the output is a filename, it will be opened for appending. If the\noutput is a buffer, all compressed data will be appended to the existing buffer.\n\nConversely when \"Append\" is not specified, or it is present and is set to false, it will\noperate as follows.\n\nWhen the output is a filename, it will truncate the contents of the file before writing any\ncompressed data. If the output is a filehandle its position will not be changed. If the\noutput is a buffer, it will be wiped before any compressed data is output.\n\nDefaults to 0.\n"
                },
                {
                    "name": "Examples",
                    "content": "Here are a few example that show the capabilities of the module.\n\nStreaming\nThis very simple command line example demonstrates the streaming capabilities of the module. The\ncode reads data from STDIN, compresses it, and writes the compressed data to STDOUT.\n\n$ echo hello world | perl -MIO::Compress::Bzip2=bzip2 -e 'bzip2 \\*STDIN => \\*STDOUT' >output.bz2\n\nThe special filename \"-\" can be used as a standin for both \"\\*STDIN\" and \"\\*STDOUT\", so the\nabove can be rewritten as\n\n$ echo hello world | perl -MIO::Compress::Bzip2=bzip2 -e 'bzip2 \"-\" => \"-\"' >output.bz2\n\nCompressing a file from the filesystem\nTo read the contents of the file \"file1.txt\" and write the compressed data to the file\n\"file1.txt.bz2\".\n\nuse strict ;\nuse warnings ;\nuse IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\n\nmy $input = \"file1.txt\";\nbzip2 $input => \"$input.bz2\"\nor die \"bzip2 failed: $Bzip2Error\\n\";\n\nReading from a Filehandle and writing to an in-memory buffer\nTo read from an existing Perl filehandle, $input, and write the compressed data to a buffer,\n$buffer.\n\nuse strict ;\nuse warnings ;\nuse IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\nuse IO::File ;\n\nmy $input = IO::File->new( \"<file1.txt\" )\nor die \"Cannot open 'file1.txt': $!\\n\" ;\nmy $buffer ;\nbzip2 $input => \\$buffer\nor die \"bzip2 failed: $Bzip2Error\\n\";\n\nCompressing multiple files\nTo compress all files in the directory \"/my/home\" that match \"*.txt\" and store the compressed\ndata in the same directory\n\nuse strict ;\nuse warnings ;\nuse IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\n\nbzip2 '</my/home/*.txt>' => '<*.bz2>'\nor die \"bzip2 failed: $Bzip2Error\\n\";\n\nand if you want to compress each file one at a time, this will do the trick\n\nuse strict ;\nuse warnings ;\nuse IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\n\nfor my $input ( glob \"/my/home/*.txt\" )\n{\nmy $output = \"$input.bz2\" ;\nbzip2 $input => $output\nor die \"Error compressing '$input': $Bzip2Error\\n\";\n}\n\nOO Interface"
                },
                {
                    "name": "Constructor",
                    "content": "The format of the constructor for \"IO::Compress::Bzip2\" is shown below\n\nmy $z = IO::Compress::Bzip2->new( $output [,OPTS] )\nor die \"IO::Compress::Bzip2 failed: $Bzip2Error\\n\";\n\nIt returns an \"IO::Compress::Bzip2\" object on success and undef on failure. The variable\n$Bzip2Error will contain an error message on failure.\n\nIf you are running Perl 5.005 or better the object, $z, returned from IO::Compress::Bzip2 can be\nused exactly like an IO::File filehandle. This means that all normal output file operations can\nbe carried out with $z. For example, to write to a compressed file/buffer you can use either of\nthese forms\n\n$z->print(\"hello world\\n\");\nprint $z \"hello world\\n\";\n\nThe mandatory parameter $output is used to control the destination of the compressed data. This\nparameter can take one of these forms.\n\nA filename\nIf the $output parameter is a simple scalar, it is assumed to be a filename. This file will\nbe opened for writing and the compressed data will be written to it.\n\nA filehandle\nIf the $output parameter is a filehandle, the compressed data will be written to it. The\nstring '-' can be used as an alias for standard output.\n\nA scalar reference\nIf $output is a scalar reference, the compressed data will be stored in $$output.\n\nIf the $output parameter is any other type, \"IO::Compress::Bzip2\"::new will return undef.\n"
                },
                {
                    "name": "Constructor Options",
                    "content": "\"OPTS\" is any combination of zero or more the following options:\n\n\"AutoClose => 0|1\"\nThis option is only valid when the $output parameter is a filehandle. If specified, and the\nvalue is true, it will result in the $output being closed once either the \"close\" method is\ncalled or the \"IO::Compress::Bzip2\" object is destroyed.\n\nThis parameter defaults to 0.\n\n\"Append => 0|1\"\nOpens $output in append mode.\n\nThe behaviour of this option is dependent on the type of $output.\n\n*    A Buffer\n\nIf $output is a buffer and \"Append\" is enabled, all compressed data will be append to\nthe end of $output. Otherwise $output will be cleared before any data is written to\nit.\n\n*    A Filename\n\nIf $output is a filename and \"Append\" is enabled, the file will be opened in append\nmode. Otherwise the contents of the file, if any, will be truncated before any\ncompressed data is written to it.\n\n*    A Filehandle\n\nIf $output is a filehandle, the file pointer will be positioned to the end of the file\nvia a call to \"seek\" before any compressed data is written to it. Otherwise the file\npointer will not be moved.\n\nThis parameter defaults to 0.\n\n\"BlockSize100K => number\"\nSpecify the number of 100K blocks bzip2 uses during compression.\n\nValid values are from 1 to 9, where 9 is best compression.\n\nThe default is 1.\n\n\"WorkFactor => number\"\nSpecifies how much effort bzip2 should take before resorting to a slower fallback\ncompression algorithm.\n\nValid values range from 0 to 250, where 0 means use the default value 30.\n\nThe default is 0.\n\n\"Strict => 0|1\"\nThis is a placeholder option.\n"
                },
                {
                    "name": "Examples",
                    "content": "TODO\n"
                }
            ]
        },
        "Methods": {
            "content": "print\nUsage is\n\n$z->print($data)\nprint $z $data\n\nCompresses and outputs the contents of the $data parameter. This has the same behaviour as the\n\"print\" built-in.\n\nReturns true if successful.\n\nprintf\nUsage is\n\n$z->printf($format, $data)\nprintf $z $format, $data\n\nCompresses and outputs the contents of the $data parameter.\n\nReturns true if successful.\n\nsyswrite\nUsage is\n\n$z->syswrite $data\n$z->syswrite $data, $length\n$z->syswrite $data, $length, $offset\n\nCompresses and outputs the contents of the $data parameter.\n\nReturns the number of uncompressed bytes written, or \"undef\" if unsuccessful.\n\nwrite\nUsage is\n\n$z->write $data\n$z->write $data, $length\n$z->write $data, $length, $offset\n\nCompresses and outputs the contents of the $data parameter.\n\nReturns the number of uncompressed bytes written, or \"undef\" if unsuccessful.\n\nflush\nUsage is\n\n$z->flush;\n\nFlushes any pending compressed data to the output file/buffer.\n\nTODO\n\nReturns true on success.\n\ntell\nUsage is\n\n$z->tell()\ntell $z\n\nReturns the uncompressed file offset.\n\neof\nUsage is\n\n$z->eof();\neof($z);\n\nReturns true if the \"close\" method has been called.\n\nseek\n$z->seek($position, $whence);\nseek($z, $position, $whence);\n\nProvides a sub-set of the \"seek\" functionality, with the restriction that it is only legal to\nseek forward in the output file/buffer. It is a fatal error to attempt to seek backward.\n\nEmpty parts of the file/buffer will have NULL (0x00) bytes written to them.\n\nThe $whence parameter takes one the usual values, namely SEEKSET, SEEKCUR or SEEKEND.\n\nReturns 1 on success, 0 on failure.\n\nbinmode\nUsage is\n\n$z->binmode\nbinmode $z ;\n\nThis is a noop provided for completeness.\n\nopened\n$z->opened()\n\nReturns true if the object currently refers to a opened file/buffer.\n\nautoflush\nmy $prev = $z->autoflush()\nmy $prev = $z->autoflush(EXPR)\n\nIf the $z object is associated with a file or a filehandle, this method returns the current\nautoflush setting for the underlying filehandle. If \"EXPR\" is present, and is non-zero, it will\nenable flushing after every write/print operation.\n\nIf $z is associated with a buffer, this method has no effect and always returns \"undef\".\n\nNote that the special variable $| cannot be used to set or retrieve the autoflush setting.\n\ninputlinenumber\n$z->inputlinenumber()\n$z->inputlinenumber(EXPR)\n\nThis method always returns \"undef\" when compressing.\n\nfileno\n$z->fileno()\nfileno($z)\n\nIf the $z object is associated with a file or a filehandle, \"fileno\" will return the underlying\nfile descriptor. Once the \"close\" method is called \"fileno\" will return \"undef\".\n\nIf the $z object is associated with a buffer, this method will return \"undef\".\n\nclose\n$z->close() ;\nclose $z ;\n\nFlushes any pending compressed data and then closes the output file/buffer.\n\nFor most versions of Perl this method will be automatically invoked if the IO::Compress::Bzip2\nobject is destroyed (either explicitly or by the variable with the reference to the object going\nout of scope). The exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these cases,\nthe \"close\" method will be called automatically, but not until global destruction of all live\nobjects when the program is terminating.\n\nTherefore, if you want your scripts to be able to run on all versions of Perl, you should call\n\"close\" explicitly and not rely on automatic closing.\n\nReturns true on success, otherwise 0.\n\nIf the \"AutoClose\" option has been enabled when the IO::Compress::Bzip2 object was created, and\nthe object is associated with a file, the underlying file will also be closed.\n\nnewStream([OPTS])\nUsage is\n\n$z->newStream( [OPTS] )\n\nCloses the current compressed data stream and starts a new one.\n\nOPTS consists of any of the options that are available when creating the $z object.\n\nSee the \"Constructor Options\" section for more details.\n",
            "subsections": []
        },
        "Importing": {
            "content": "No symbolic constants are required by IO::Compress::Bzip2 at present.\n\n:all Imports \"bzip2\" and $Bzip2Error. Same as doing this\n\nuse IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "",
            "subsections": [
                {
                    "name": "Apache::GZip Revisited",
                    "content": "See IO::Compress::FAQ\n"
                },
                {
                    "name": "Working with Net::FTP",
                    "content": "See IO::Compress::FAQ\n"
                }
            ]
        },
        "SUPPORT": {
            "content": "General feedback/questions/bug reports should be sent to\n<https://github.com/pmqs/IO-Compress/issues> (preferred) or\n<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip, IO::Compress::Deflate,\nIO::Uncompress::Inflate, IO::Compress::RawDeflate, IO::Uncompress::RawInflate,\nIO::Uncompress::Bunzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma, IO::Compress::Xz,\nIO::Uncompress::UnXz, IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop,\nIO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,\nIO::Uncompress::UnZstd, IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompress\n\nIO::Compress::FAQ\n\nFile::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib\n\nThe primary site for the bzip2 program is <https://sourceware.org/bzip2/>.\n\nSee the module Compress::Bzip2\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "This module was written by Paul Marquess, \"pmqs@cpan.org\".\n",
            "subsections": []
        },
        "MODIFICATION HISTORY": {
            "content": "See the Changes file.\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "Copyright (c) 2005-2021 Paul Marquess. All rights reserved.\n\nThis program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        }
    },
    "summary": "IO::Compress::Bzip2 - Write bzip2 files/buffers",
    "flags": [],
    "examples": [
        "See IO::Compress::FAQ",
        "See IO::Compress::FAQ"
    ],
    "see_also": []
}