Deflate 格式数据的压缩和解压
import compress.zlib.*
import std.fs.*
main() {
var arr: Array<Byte> = Array<Byte>(1024 * 1024, {i => UInt8(i % 256)})
File.writeTo("./zlib1.txt", arr, openOption: Create(false))
if (compressFile("./zlib1.txt", "./zlib_copmressed1.zlib") <= 0) {
println("Failed to compress file!")
}
if (decompressFile("./zlib_copmressed1.zlib", "./zlib_decopmressed1.txt") != arr.size) {
println("Failed to decompress file!")
}
if (compareFile("./zlib1.txt", "./zlib_decopmressed1.txt")) {
println("success")
} else {
println("failed")
}
remove("./zlib1.txt")
remove("./zlib_copmressed1.zlib")
remove("./zlib_decopmressed1.txt")
return 0
}
func compressFile(srcFileName: String, destFileName: String): Int64 {
var count: Int64 = 0
var srcFile: File = File(srcFileName, OpenOption.Open(true, false))
var destFile: File = File(destFileName, OpenOption.Create(false))
var tempBuf: Array<UInt8> = Array<UInt8>(1024, repeat: 0)
var compressOutputStream: CompressOutputStream = CompressOutputStream(destFile, wrap: DeflateFormat)
while (true) {
var readNum = srcFile.read(tempBuf)
if (readNum > 0) {
compressOutputStream.write(tempBuf.slice(0, readNum).toArray())
count += readNum
} else {
break
}
}
compressOutputStream.flush()
compressOutputStream.close()
srcFile.close()
destFile.close()
return count
}
func decompressFile(srcFileName: String, destFileName: String): Int64 {
var count: Int64 = 0
var srcFile: File = File(srcFileName, OpenOption.Open(true, false))
var destFile: File = File(destFileName, OpenOption.Create(false))
var tempBuf: Array<UInt8> = Array<UInt8>(1024, repeat: 0)
var decompressInputStream: DecompressInputStream = DecompressInputStream(srcFile, wrap: DeflateFormat)
while (true) {
var readNum = decompressInputStream.read(tempBuf)
if (readNum <= 0) {
break
}
destFile.write(tempBuf.slice(0, readNum).toArray())
count += readNum
}
decompressInputStream.close()
srcFile.close()
destFile.close()
return count
}
func compareFile(fileName1: String, fileName2: String): Bool {
return File.readFrom(fileName1) == File.readFrom(fileName2)
}
运行结果
success