好得很程序员自学网

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

Java多文件以ZIP压缩包导出的实现方法

本文实例为大家分享了java多文件以zip 压缩包 导出 的具体代码,供大家参考,具体内容如下

1、使用java实现吧服务器的图片打包成一个zip格式的压缩包导出,多个文件打包导出。
2、代码如下:

**imagebyteutil.java**

?

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

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

public class imagebyteutil{

   private static float quality = 0 .6f;

   public static void compresszip(list<file> listfiles, outputstream output,string encode, boolean compress,string alias){

   zipoutputstream zipstream = null ;

   try {

       zipstream = new zipoutputstream(output);

       for (file file : listfiles){

         compresszip(file, zipstream, compress,alias+ "_" +(listfiles.indexof(file)+ 1 ));

       }

     } catch (exception e) {

       e.printstacktrace();

     } finally {

       try {

         if (zipstream != null ) {

           zipstream.close();

         }

       } catch (ioexception e) {

         e.printstacktrace();

       }

     }

   }

 

private static void compresszip(file file, zipoutputstream zipstream,

       boolean compress,string alias) throws exception{

     fileinputstream input = null ;

     try {

       input = new fileinputstream(file);

       //zip(input, zipstream, file.getname(), compress);

       zip(input, zipstream, alias+ "." +file.getname().substring(file.getname().lastindexof( "." )+ 1 ), compress);

     } catch (exception e) {

       e.printstacktrace();

     } finally {

       try {

         if (input != null )

           input.close();

       } catch (ioexception e) {

         e.printstacktrace();

       }

     }

   }

 

private static void zip(inputstream input, zipoutputstream zipstream,

       string zipentryname, boolean compress) throws exception{

       byte [] bytes = null ;

     bufferedinputstream bufferstream = null ;

     try {

       if (input == null )

         throw new exception( "获取压缩的数据项失败! 数据项名为:" + zipentryname);

       // 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样

       zipentry zipentry = new zipentry( "图片/" +zipentryname);

       // 定位到该压缩条目位置,开始写入文件到压缩包中

       zipstream.putnextentry(zipentry);

       if (compress) {

         bytes = imagebyteutil.compressofquality(input, 0 );

         zipstream.write(bytes, 0 , bytes.length);

       } else {

         bytes = new byte [ 1024 * 5 ]; // 读写缓冲区

         bufferstream = new bufferedinputstream(input); // 输入缓冲流

         int read = 0 ;

         while ((read = bufferstream.read(bytes)) != - 1 ) {

           zipstream.write(bytes, 0 , read);

         }

       }

     } catch (ioexception e) {

       e.printstacktrace();

     } finally {

       try {

         if ( null != bufferstream)

           bufferstream.close();

       } catch (ioexception e) {

         e.printstacktrace();

       }

     }

   }

 

   public static byte [] compressofquality(file file, float quality) throws exception{

     byte [] bs = null ;

     inputstream input = null ;

     try {

       input = new fileinputstream(file);

       bs = compressofquality(input,quality);

     } catch (exception e) {

       e.printstacktrace();

     } finally {

       try {

         if (input != null )

           input.close();

       } catch (ioexception e) {

         e.printstacktrace();

       }

     }

     return bs;

   }

 

   public static byte [] compressofquality(inputstream input, float quality)

       throws exception {

       bytearrayoutputstream output = null ;

     try {

       output = new bytearrayoutputstream();

       if (quality == 0 ){

         thumbnails.of(input).scale(1f).outputquality(quality)

         .tooutputstream(output);

       } else {

         thumbnails.of(input).scale(1f).outputquality(quality).tooutputstream(output);

       }

       return output.tobytearray();

     } catch (exception e) {

       e.printstacktrace();

     } finally {

       try {

         if (output != null )

           output.close();

       } catch (ioexception e) {

         e.printstacktrace();

       }

     }

     return null ;

   }

}

**main.java**

?

1

2

3

4

5

6

7

8

9

10

public static void main(string[] args){

   //要导出的文件集合,添加自己需要导出的文件

   list<file> listfiles = new arraylist<>();

   //调用工具类,参数说明(需要导出的文件集,bytearrayoutputstream对象,编码,是否压缩【true,false】,文件名称前缀)

   imagebyteutil.compresszip(listfiles, out, "utf-8" , false , "lwj" );

   //指定导出格式

   returncontext.addparam( "exportfilename" , "extfile.zip" );

   returncontext.addparam( "mimetype" , "zip" );

   return in;

}

3、此功能是根据在开发过程中项目需要实现的,测试可正常使用,可更改定制。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/lovelongjun/article/details/76104192

查看更多关于Java多文件以ZIP压缩包导出的实现方法的详细内容...

  阅读:86次