目标是要存下传来的 GZIP 文件流,当然得解压好。iOS 做这件事是自觉自愿,Android 不做。于是我找啊找。如果 Native.js 也不行,那该去写原生插件了吧。结果确实不行,因为没法用上 buffer,一张图片几分钟才出来。讨论群里答疑说 Native.js 不是这么用的。呃,好吧。
最后我也没写插件,一时半会写不出。
handleAndroid(file, inFilePath, targetDir, fileName) {
// #ifdef APP-PLUS
var absInPath = plus.io.convertLocalFileSystemURL( inFilePath )
var absOutPath = plus.io.convertLocalFileSystemURL( targetDir )
console.log("in 路径:", absInPath)
console.log("out 路径:", absOutPath)
var File = plus.android.importClass("java.io.File")
var FileInputStream = plus.android.importClass("java.io.FileInputStream");
var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
var BufferedInputStream = plus.android.importClass("java.io.BufferedInputStream");
var BufferedOutputStream = plus.android.importClass("java.io.BufferedOutputStream");
var GZIPInputStream = plus.android.importClass("java.util.zip.GZIPInputStream");
var inFile = new File(absInPath)
console.log("inFile: ", inFile.canRead(), inFile.canWrite(), inFile.isFile(), inFile.length(), inFile.getAbsolutePath())
if(inFile.exists()) {
var ins = new BufferedInputStream( new GZIPInputStream(new FileInputStream(absInPath)) )
var directory = new File(absOutPath);
if(!directory.exists()) {
directory.mkdirs(); //创建目录
}
var outFile = new File(absOutPath + fileName);
if(!outFile.exists()) {
outFile.createNewFile(); //创建文件
}
var os = new BufferedOutputStream( new FileOutputStream(absOutPath + fileName) )
console.log("outFile: ", outFile.canRead(), outFile.canWrite(), outFile.isFile(), outFile.length(), outFile.getAbsolutePath())
// var buf = new ArrayBuffer(1024)
// console.log("===", ins.read(buf))
var n
let count = 0
while ((n = ins.read()) != -1) { // 单字节
os.write(n);
if(count%2000 == 0) {
console.log("count: ", count)
uni.showToast({
icon: 'loading',
title: String(count)
})
}
count++
}
console.log("outFile: ", outFile.canRead(), outFile.canWrite(), outFile.isFile(), outFile.length(), outFile.getAbsolutePath())
ins.close()
os.close()
} else {
console.log("文件不存在")
}
// #endif
},