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

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

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

 

 

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

 

 

文件的压缩

 1 public class TestFile
 2 {
 3     public static void main ( String [ ] args ) throws IOException
 4     {
 5         // new a file input stream
 6         FileInputStream fis = new FileInputStream (
 7                 "/home/liangruihua/ziptest/1.txt" ) ;
 8         BufferedInputStream bis = new BufferedInputStream ( fis ) ;
 9 
10         // new a zipPutputStream
11         // /home/liangruihua/ziptest/1.zip -- the out put file path and
12         // name
13         ZipOutputStream zos = new ZipOutputStream (
14                 new FileOutputStream (
15                         "/home/liangruihua/ziptest/1.zip" ) ) ;
16         BufferedOutputStream bos = new BufferedOutputStream ( zos ) ;
17 
18         // set the file name in the .zip file
19         zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ;
20 
21         // set the declear
22         zos.setComment ( "by liangruihua test!" ) ;
23 
24         byte [ ] b = new byte [ 100 ] ;
25         while ( true )
26         {
27             int len = bis.read ( b ) ;
28             if ( len == - 1 )
29                 break ;
30             bos.write ( b , 0 , len ) ;
31         }
32         fis.close ( ) ;
33         zos.close ( ) ;
34     }
35 }

 

文件夹的压缩

 1 public class TestDir
 2 {
 3     public static void main ( String [ ] args ) throws IOException
 4     {
 5         // the file path need to compress
 6         File file = new File ( "/home/liangruihua/ziptest/test" ) ;
 7         ZipOutputStream zos = new ZipOutputStream (
 8                 new FileOutputStream (
 9                         "/home/liangruihua/ziptest/test.zip" ) ) ;
10 
11         // judge the file is the directory
12         if ( file.isDirectory ( ) )
13         {
14             // get the every file in the directory
15             File [ ] files = file.listFiles ( ) ;
16 
17             for ( int i = 0 ; i < files.length ; i ++ )
18             {
19                 // new the BuuferedInputStream
20                 BufferedInputStream bis = new BufferedInputStream (
21                         new FileInputStream (
22                                 files [ i ] ) ) ;
23                 // the file entry ,set the file name in the zip
24                 // file
25                 zos.putNextEntry ( new ZipEntry ( file
26                         .getName ( )
27                         + file.separator
28                         + files [ i ].getName ( ) ) ) ;
29                 while ( true )
30                 {
31                     byte [ ] b = new byte [ 100 ] ;
32                     int len = bis.read ( b ) ;
33                     if ( len == - 1 )
34                         break ;
35                     zos.write ( b , 0 , len ) ;
36                 }
37 
38                 // close the input stream
39                 bis.close ( ) ;
40             }
41 
42         }
43         // close the zip output stream
44         zos.close ( ) ;
45     }
46 }

文件的解压

 1 public class TestZipInputStream
 2 {
 3     public static void main ( String [ ] args ) throws ZipException ,
 4             IOException
 5     {
 6         // get a zip file instance
 7         File file = new File ( "/home/liangruihua/ziptest/test.zip" ) ;
 8 
 9         // get a ZipFile instance
10         ZipFile zipFile = new ZipFile ( file ) ;
11 
12         // create a ZipInputStream instance
13         ZipInputStream zis = new ZipInputStream ( new FileInputStream (
14                 file ) ) ;
15 
16         // create a ZipEntry instance , lay the every file from
17         // decompress file temporarily
18         ZipEntry entry = null ;
19 
20         // a circle to get every file
21         while ( ( entry = zis.getNextEntry ( ) ) != null )
22         {
23             System.out.println ( "decompress file :"
24                     + entry.getName ( ) ) ;
25 
26             // define the path to set the file
27             File outFile = new File ( "/home/liangruihua/ziptest/"
28                     + entry.getName ( ) ) ;
29 
30             // if the file's parent directory wasn't exits ,than
31             // create the directory
32             if ( ! outFile.getParentFile ( ).exists ( ) )
33             {
34                 outFile.getParentFile ( ).mkdir ( ) ;
35             }
36 
37             // if the file not exits ,than create the file
38             if ( ! outFile.exists ( ) )
39             {
40                 outFile.createNewFile ( ) ;
41             }
42 
43             // create an input stream
44             BufferedInputStream bis = new BufferedInputStream (
45                     zipFile.getInputStream ( entry ) ) ;
46 
47             // create an output stream
48             BufferedOutputStream bos = new BufferedOutputStream (
49                     new FileOutputStream ( outFile ) ) ;
50             byte [ ] b = new byte [ 100 ] ;
51             while ( true )
52             {
53                 int len = bis.read ( b ) ;
54                 if ( len == - 1 )
55                     break ;
56                 bos.write ( b , 0 , len ) ;
57             }
58             // close stream
59             bis.close ( ) ;
60             bos.close ( ) ;
61         }
62         zis.close ( ) ;
63 
64     }
65 }

 

转载于:https://www.cnblogs.com/lrh-xl/p/5509005.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以通过使用ZipOutputStreamZipInputStream类来实现文件压缩解压缩,但是这两个类只支持Zip格式的压缩文件。对于PDF格式的文件,可以使用iText库来实现压缩解压缩。 以下是一个使用iText库实现PDF文件压缩解压缩的示例代码: ```java import java.io.*; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; public class PDFCompressor { public static void compressPdf(String srcFile, String destFile) throws Exception { PdfReader reader = new PdfReader(srcFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile)); PdfCompressionSettings compression = new PdfCompressionSettings(); compression.setCompressionLevel(PdfStream.BEST_COMPRESSION); stamper.setCompressionLevel(9); stamper.setFullCompression(); stamper.getWriter().setCompressionLevel(9); stamper.getWriter().setCompressionLevel(9, compression); stamper.close(); reader.close(); } public static void decompressPdf(String srcFile, String destFile) throws Exception { PdfReader.unethicalreading = true; PdfReader reader = new PdfReader(srcFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile)); stamper.getWriter().setCompressionLevel(0); stamper.close(); reader.close(); } public static void main(String[] args) { try { compressPdf("example.pdf", "example_compressed.pdf"); decompressPdf("example_compressed.pdf", "example_decompressed.pdf"); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中,compressPdf方法用于压缩PDF文件,decompressPdf方法用于解压缩PDF文件。在compressPdf方法中,我们使用PdfStamper类来设置PDF文件压缩级别和压缩方式。在decompressPdf方法中,我们将PDF文件压缩级别设置为0,以实现解压缩。 需要注意的是,iText库需要额外下载并添加到项目中。可以从官网https://itextpdf.com/zh/download下载最新版本的iText库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值