java 文件压缩和解压(ZipInputStream, ZipOutputStream)

时间:2023-01-10 11:28:56

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

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

java 文件压缩和解压(ZipInputStream, ZipOutputStream)

java 文件压缩和解压(ZipInputStream, ZipOutputStream)

java 文件压缩和解压(ZipInputStream, ZipOutputStream)

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

java 文件压缩和解压(ZipInputStream, ZipOutputStream)

java 文件压缩和解压(ZipInputStream, ZipOutputStream)

java 文件压缩和解压(ZipInputStream, ZipOutputStream)

文件的压缩

 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 ( ) ; }
}