|
@@ -0,0 +1,131 @@
|
|
|
+package com.doverfuelingsolutions.issp.utils
|
|
|
+
|
|
|
+import com.doverfuelingsolutions.issp.utils.log.DFSLog
|
|
|
+import java.io.File
|
|
|
+import java.io.FileInputStream
|
|
|
+import java.io.IOException
|
|
|
+import java.io.OutputStream
|
|
|
+import java.util.zip.ZipEntry
|
|
|
+import java.util.zip.ZipOutputStream
|
|
|
+
|
|
|
+object ZipUtils {
|
|
|
+
|
|
|
+ private const val BUFFER_SIZE = 2048
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩成ZIP方法
|
|
|
+ *
|
|
|
+ * @param srcDir 待压缩目录或者文件路径
|
|
|
+ * @param targetFile 压缩输出文件路径
|
|
|
+ * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
|
|
+ * @throws RuntimeException 压缩失败会抛出运行时异常
|
|
|
+ */
|
|
|
+ fun buildZip(srcDir: String, targetFile: String, KeepDirStructure: Boolean): Boolean {
|
|
|
+ val out = File(targetFile).outputStream()
|
|
|
+ var zos: ZipOutputStream? = null
|
|
|
+ try {
|
|
|
+ val sourceFile = File(srcDir)
|
|
|
+ zos = ZipOutputStream(out)
|
|
|
+ compress(sourceFile, zos, sourceFile.name, KeepDirStructure)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ DFSLog.e(e)
|
|
|
+ return false
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ zos?.close()
|
|
|
+ out.close()
|
|
|
+ } catch (e: IOException) {
|
|
|
+ DFSLog.e(e)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩成ZIP 方法
|
|
|
+ *
|
|
|
+ * @param srcFiles 需要压缩的文件列表 58
|
|
|
+ * @param out 压缩文件输出流 59
|
|
|
+ * @throws RuntimeException 压缩失败会抛出运行时异常 60
|
|
|
+ */
|
|
|
+ @Throws(RuntimeException::class)
|
|
|
+ fun toZip(srcFiles: List<File>, out: OutputStream?) {
|
|
|
+ val start = System.currentTimeMillis()
|
|
|
+ var zos: ZipOutputStream? = null
|
|
|
+ try {
|
|
|
+ zos = ZipOutputStream(out)
|
|
|
+ for (srcFile in srcFiles) {
|
|
|
+ val buf = ByteArray(BUFFER_SIZE)
|
|
|
+ zos.putNextEntry(ZipEntry(srcFile.name))
|
|
|
+ read(zos, srcFile, buf)
|
|
|
+ }
|
|
|
+ val end = System.currentTimeMillis()
|
|
|
+ println("压缩完成,耗时:" + (end - start) + " ms")
|
|
|
+ } catch (e: Exception) {
|
|
|
+ throw RuntimeException("zip error from ZipUtils", e)
|
|
|
+ } finally {
|
|
|
+ if (zos != null) {
|
|
|
+ try {
|
|
|
+ zos.close()
|
|
|
+ } catch (e: IOException) {
|
|
|
+ e.printStackTrace()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归压缩方法
|
|
|
+ *
|
|
|
+ * @param sourceFile 源文件
|
|
|
+ * @param zos zip输出流
|
|
|
+ * @param name 压缩后的名称
|
|
|
+ * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构
|
|
|
+ * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Throws(Exception::class)
|
|
|
+ private fun compress(sourceFile: File, zos: ZipOutputStream, name: String, KeepDirStructure: Boolean) {
|
|
|
+ val buf = ByteArray(BUFFER_SIZE)
|
|
|
+ if (sourceFile.isFile) {
|
|
|
+ // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
|
|
|
+ zos.putNextEntry(ZipEntry("$name/$name"))
|
|
|
+ // copy文件到zip输出流中
|
|
|
+ read(zos, sourceFile, buf)
|
|
|
+ } else {
|
|
|
+ val listFiles = sourceFile.listFiles()
|
|
|
+ if (listFiles == null || listFiles.isEmpty()) {
|
|
|
+ // 需要保留原来的文件结构时,需要对空文件夹进行处理
|
|
|
+ if (KeepDirStructure) {
|
|
|
+ // 空文件夹的处理
|
|
|
+ zos.putNextEntry(ZipEntry("$name/"))
|
|
|
+ // 没有文件,不需要文件的copy
|
|
|
+ zos.closeEntry()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (file in listFiles) {
|
|
|
+ // 判断是否需要保留原来的文件结构
|
|
|
+ if (KeepDirStructure) {
|
|
|
+ // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
|
|
|
+ // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
|
|
|
+ compress(file, zos, name + "/" + file.name, KeepDirStructure)
|
|
|
+ } else {
|
|
|
+ compress(file, zos, file.name, KeepDirStructure)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Throws(IOException::class)
|
|
|
+ private fun read(zos: ZipOutputStream, srcFile: File, buf: ByteArray) {
|
|
|
+ var len: Int
|
|
|
+ val inputStream = FileInputStream(srcFile)
|
|
|
+ while (inputStream.read(buf).also { len = it } != -1) {
|
|
|
+ zos.write(buf, 0, len)
|
|
|
+ }
|
|
|
+ zos.closeEntry()
|
|
|
+ inputStream.close()
|
|
|
+ }
|
|
|
+}
|