1+ package com.wrbug.developerhelper.commonutil
2+
3+ import java.io.*
4+ import java.util.zip.ZipEntry
5+ import java.util.zip.ZipFile
6+ import java.util.zip.ZipOutputStream
7+
8+ object ZipUtils {
9+ fun zip (src : File , outFile : File ) {
10+ // 提供了一个数据项压缩成一个ZIP归档输出流
11+ var out : ZipOutputStream ? = null
12+ try {
13+ out = ZipOutputStream (FileOutputStream (outFile))
14+ // 如果此文件是一个文件,否则为false。
15+ if (src.isFile) {
16+ zipFileOrDirectory(out , src, " " )
17+ } else {
18+ // 返回一个文件或空阵列。
19+ val entries = src.listFiles()
20+ for (i in entries!! .indices) {
21+ // 递归压缩,更新curPaths
22+ zipFileOrDirectory(out , entries[i], " " )
23+ }
24+ }
25+ } catch (ex: IOException ) {
26+ ex.printStackTrace()
27+ } finally {
28+ // 关闭输出流
29+ if (out != null ) {
30+ try {
31+ out .close()
32+ } catch (ex: IOException ) {
33+ ex.printStackTrace()
34+ }
35+
36+ }
37+ }
38+ }
39+
40+ fun zip (src : File ): File ? {
41+ val outFile = File (src.parent, src.name + " .zip" )// 源文件或者目录
42+ zip(src, outFile)
43+ return outFile
44+ }
45+
46+ @Throws(IOException ::class )
47+ private fun zipFileOrDirectory (
48+ out : ZipOutputStream ,
49+ fileOrDirectory : File , curPath : String
50+ ) {
51+ // 从文件中读取字节的输入流
52+ var fileInputStream: FileInputStream ? = null
53+ try {
54+ // 如果此文件是一个目录,否则返回false。
55+ if (! fileOrDirectory.isDirectory) {
56+ // 压缩文件
57+ val buffer = ByteArray (4096 )
58+ fileInputStream = FileInputStream (fileOrDirectory)
59+ // 实例代表一个条目内的ZIP归档
60+ val entry = ZipEntry (curPath + fileOrDirectory.name)
61+ // 条目的信息写入底层流
62+ out .putNextEntry(entry)
63+ var bytesRead: Int = fileInputStream.read(buffer)
64+ while (bytesRead != - 1 ) {
65+ out .write(buffer, 0 , bytesRead)
66+ bytesRead = fileInputStream.read(buffer)
67+ }
68+ out .closeEntry()
69+ } else {
70+ // 压缩目录
71+ val entries = fileOrDirectory.listFiles()
72+ for (i in entries!! .indices) {
73+ // 递归压缩,更新curPaths
74+ zipFileOrDirectory(
75+ out , entries[i], curPath
76+ + fileOrDirectory.name + " /"
77+ )
78+ }
79+ }
80+ } catch (ex: IOException ) {
81+ ex.printStackTrace()
82+ // throw ex;
83+ } finally {
84+ if (fileInputStream != null ) {
85+ try {
86+ fileInputStream.close()
87+ } catch (ex: IOException ) {
88+ ex.printStackTrace()
89+ }
90+
91+ }
92+ }
93+ }
94+
95+ @Throws(IOException ::class )
96+ fun unzip (zipFileName : String , outputDirectory : String ) {
97+ var zipFile: ZipFile ? = null
98+ try {
99+ zipFile = ZipFile (zipFileName)
100+ val e = zipFile.entries()
101+ var zipEntry: ZipEntry ? = null
102+ val dest = File (outputDirectory)
103+ dest.mkdirs()
104+ while (e.hasMoreElements()) {
105+ zipEntry = e.nextElement() as ZipEntry
106+ val entryName = zipEntry.name
107+ var inputStream: InputStream ? = null
108+ var out : FileOutputStream ? = null
109+ try {
110+ if (zipEntry.isDirectory) {
111+ var name = zipEntry.name
112+ name = name.substring(0 , name.length - 1 )
113+ val f = File (
114+ outputDirectory + File .separator
115+ + name
116+ )
117+ f.mkdirs()
118+ } else {
119+ var index = entryName.lastIndexOf(" \\ " )
120+ if (index != - 1 ) {
121+ val df = File (
122+ outputDirectory + File .separator
123+ + entryName.substring(0 , index)
124+ )
125+ df.mkdirs()
126+ }
127+ index = entryName.lastIndexOf(" /" )
128+ if (index != - 1 ) {
129+ val df = File (
130+ outputDirectory + File .separator
131+ + entryName.substring(0 , index)
132+ )
133+ df.mkdirs()
134+ }
135+ val f = File (
136+ outputDirectory + File .separator
137+ + zipEntry.name
138+ )
139+ // f.createNewFile();
140+ inputStream = zipFile.getInputStream(zipEntry)
141+ out = FileOutputStream (f)
142+ val by = ByteArray (1024 )
143+ var c: Int = inputStream?.read(by) ? : - 1
144+ while (c != - 1 ) {
145+ out .write(by, 0 , c)
146+ c = inputStream?.read(by) ? : - 1
147+ }
148+ out .flush()
149+ }
150+ } catch (ex: IOException ) {
151+ ex.printStackTrace()
152+ throw IOException (" 解压失败:" + ex.toString())
153+ } finally {
154+ if (inputStream != null ) {
155+ try {
156+ inputStream.close()
157+ } catch (ex: IOException ) {
158+ }
159+
160+ }
161+ if (out != null ) {
162+ try {
163+ out .close()
164+ } catch (ex: IOException ) {
165+ }
166+
167+ }
168+ }
169+ }
170+ } catch (ex: IOException ) {
171+ ex.printStackTrace()
172+ throw IOException (" 解压失败:" + ex.toString())
173+ } finally {
174+ if (zipFile != null ) {
175+ try {
176+ zipFile.close()
177+ } catch (ex: IOException ) {
178+ }
179+
180+ }
181+ }
182+ }
183+ }
184+
185+
186+ fun File.zip (): File ? {
187+ return ZipUtils .zip(this )
188+ }
189+
190+
191+ fun File.zip (outFile : File ) {
192+ return ZipUtils .zip(this , outFile)
193+ }
0 commit comments