好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现把文件压缩成zip文件的示例代码

实现代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

ackage org.fh.util;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

 

/**

  * 说明:java压缩成zip

  * 作者:FH Admin

  * from:fhadmin.cn

  */

public class FileZip {

 

     /**

      * @param inputFileName 你要压缩的文件夹(整个完整路径)

      * @param zipFileName 压缩后的文件(整个完整路径)

      * @throws Exception

      */

     public static Boolean zip(String inputFileName, String zipFileName) throws Exception {

         zip(zipFileName, new File(inputFileName));

         return true ;

     }

 

     private static void zip(String zipFileName, File inputFile) throws Exception {

         ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFileName));

         zip(out, inputFile, "" );

         out.flush();

         out.close();

     }

 

     private static void zip(ZipOutputStream out, File f, String base) throws Exception {

         if (f.isDirectory()) {

             File[] fl = f.listFiles();

             out.putNextEntry( new ZipEntry(base + "/" ));

             base = base.length() == 0 ? "" : base + "/" ;

             for ( int i = 0 ; i < fl.length; i++) {

                 zip(out, fl[i], base + fl[i].getName());

             }

         } else {

             out.putNextEntry( new ZipEntry(base));

             FileInputStream in = new FileInputStream(f);

             int b;

             while ((b = in.read()) != - 1 ) {

                 out.write(b);

             }

             in.close();

         }

     }

    

      public static void main(String [] temp){      

          try {          

              zip( "E:\\ftl" , "E:\\test.zip" ); //你要压缩的文件夹      和  压缩后的文件

              } catch (Exception ex) {      

                  ex.printStackTrace();   

              }  

         }

}

代码解释:

1.模型管理 :web在线流程设计器、导入导出xml、复制流程、部署流程

2.流程管理 :导入导出流程资源文件、查看流程图、根据流程实例反射出流程模型、激活挂起

3.运行中流程:查看流程信息、当前任务节点、当前流程图、作废暂停流程、指派待办人、自由跳转

4.历史的流程:查看流程信息、流程用时、流程状态、查看任务发起人信息

5.待办任务 :查看本人个人任务以及本角色下的任务、办理、驳回、作废、指派一下代理人

6.已办任务 :查看自己办理过的任务以及流程信息、流程图、流程状态(作废 驳回 正常完成)

补充

当然Java不仅能实现将文件压缩成zip文件,还可以实现将zip文件解压

下面是实现的工具类的核心代码,可以参考一下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

/**

  * 解压到指定目录

  * @param zipPath

  * @param descDir

  * @author isea533

  */

  public static void unZipFiles(String zipPath,String descDir) throws IOException{

  unZipFiles( new File(zipPath), descDir);

  }

  /**

  * 解压文件到指定目录

  * @param zipFile

  * @param descDir

  * @author isea533

  */

  @SuppressWarnings ( "rawtypes" )

  public static void unZipFiles(File zipFile,String descDir) throws IOException{

  File pathFile = new File(descDir);

  if (!pathFile.exists()){

   pathFile.mkdirs();

  }

  ZipFile zip = new ZipFile(zipFile);

  for (Enumeration entries = zip.getEntries();entries.hasMoreElements();){

   ZipEntry entry = (ZipEntry)entries.nextElement();

   String zipEntryName = entry.getName();

   InputStream in = zip.getInputStream(entry);

   String outPath = (descDir+zipEntryName).replaceAll( "\\*" , "/" );;

   //判断路径是否存在,不存在则创建文件路径

   File file = new File(outPath.substring( 0 , outPath.lastIndexOf( '/' )));

   if (!file.exists()){

   file.mkdirs();

   }

   //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压

   if ( new File(outPath).isDirectory()){

   continue ;

   }

   //输出文件路径信息

   System.out.println(outPath);

  

   OutputStream out = new FileOutputStream(outPath);

   byte [] buf1 = new byte [ 1024 ];

   int len;

   while ((len=in.read(buf1))> 0 ){

   out.write(buf1, 0 ,len);

   }

   in.close();

   out.close();

   }

  System.out.println( "******************解压完毕********************" );

  }

到此这篇关于Java实现把文件压缩成zip文件的示例代码的文章就介绍到这了,更多相关Java文件压缩成zip内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/m17054598469/p/15929394.html

查看更多关于Java实现把文件压缩成zip文件的示例代码的详细内容...

  阅读:18次