JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)

最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还可以对文件夹进行压缩和解压。

  ZipInputStream位于java.util.zip包下。下面是它的API,比较简单。

文件的压缩

public class TestFile
{
    public static void main ( String [ ] args ) throws IOException
    {
        // new a file input stream
        FileInputStream fis = new FileInputStream (
                "/home/liangruihua/ziptest/1.txt" ) ;
        BufferedInputStream bis = new BufferedInputStream ( fis ) ;

        // new a zipPutputStream
        // /home/liangruihua/ziptest/1.zip -- the out put file path and
        // name
        ZipOutputStream zos = new ZipOutputStream (
                new FileOutputStream (
                        "/home/liangruihua/ziptest/1.zip" ) ) ;
        BufferedOutputStream bos = new BufferedOutputStream ( zos ) ;

        // set the file name in the .zip file
        zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ;

        // set the declear
        zos.setComment ( "by liangruihua test!" ) ;

        byte [ ] b = new byte [ 100 ] ;
        while ( true )
        {
            int len = bis.read ( b ) ;
            if ( len == - 1 )
                break ;
            bos.write ( b , 0 , len ) ;
        }
        fis.close ( ) ;
        zos.close ( ) ;
    }
}


文件夹的压缩

public class TestDir
{
    public static void main ( String [ ] args ) throws IOException
    {
        // the file path need to compress
        File file = new File ( "/home/liangruihua/ziptest/test" ) ;
        ZipOutputStream zos = new ZipOutputStream (
                new FileOutputStream (
                        "/home/liangruihua/ziptest/test.zip" ) ) ;

        // judge the file is the directory
        if ( file.isDirectory ( ) )
        {
            // get the every file in the directory
            File [ ] files = file.listFiles ( ) ;

            for ( int i = 0 ; i < files.length ; i ++ )
            {
                // new the BuuferedInputStream
                BufferedInputStream bis = new BufferedInputStream (
                        new FileInputStream (
                                files [ i ] ) ) ;
                // the file entry ,set the file name in the zip
                // file
                zos.putNextEntry ( new ZipEntry ( file
                        .getName ( )
                        + file.separator
                        + files [ i ].getName ( ) ) ) ;
                while ( true )
                {
                    byte [ ] b = new byte [ 100 ] ;
                    int len = bis.read ( b ) ;
                    if ( len == - 1 )
                        break ;
                    zos.write ( b , 0 , len ) ;
                }

                // close the input stream
                bis.close ( ) ;
            }

        }
        // close the zip output stream
        zos.close ( ) ;
    }
}


文件的解压

public class TestZipInputStream
{
    public static void main ( String [ ] args ) throws ZipException ,
            IOException
    {
        // get a zip file instance
        File file = new File ( "/home/liangruihua/ziptest/test.zip" ) ;

        // get a ZipFile instance
        ZipFile zipFile = new ZipFile ( file ) ;

        // create a ZipInputStream instance
        ZipInputStream zis = new ZipInputStream ( new FileInputStream (
                file ) ) ;

        // create a ZipEntry instance , lay the every file from
        // decompress file temporarily
        ZipEntry entry = null ;

        // a circle to get every file
        while ( ( entry = zis.getNextEntry ( ) ) != null )
        {
            System.out.println ( "decompress file :"
                    + entry.getName ( ) ) ;

            // define the path to set the file
            File outFile = new File ( "/home/liangruihua/ziptest/"
                    + entry.getName ( ) ) ;

            // if the file's parent directory wasn't exits ,than
            // create the directory
            if ( ! outFile.getParentFile ( ).exists ( ) )
            {
                outFile.getParentFile ( ).mkdir ( ) ;
            }

            // if the file not exits ,than create the file
            if ( ! outFile.exists ( ) )
            {
                outFile.createNewFile ( ) ;
            }

            // create an input stream
            BufferedInputStream bis = new BufferedInputStream (
                    zipFile.getInputStream ( entry ) ) ;

            // create an output stream
            BufferedOutputStream bos = new BufferedOutputStream (
                    new FileOutputStream ( outFile ) ) ;
            byte [ ] b = new byte [ 100 ] ;
            while ( true )
            {
                int len = bis.read ( b ) ;
                if ( len == - 1 )
                    break ;
                bos.write ( b , 0 , len ) ;
            }
            // close stream
            bis.close ( ) ;
            bos.close ( ) ;
        }
        zis.close ( ) ;

    }
}


 本文转载自:博客园文章:JAVA 文件压缩和解压(ZIPINPUTSTREAM, ZIPOUTPUTSTREAM)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值