fix: 修改部分情况下分包数据与总数据大小不一致的情况

This commit is contained in:
xiaoyan 2023-06-05 13:56:40 +08:00
parent 4ea20cb0cf
commit 81ec86bb04
3 changed files with 38 additions and 7 deletions

View File

@ -37,8 +37,8 @@ android {
applicationId "com.navinfo.outdoor"
minSdkVersion 24
targetSdkVersion 30
versionCode 55
versionName "8.230518-正式版-OCR"
versionCode 102
versionName "8.230605-正式版-OCR"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {

View File

@ -7,6 +7,7 @@ import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.elvishew.xlog.XLog;
import com.github.lazylibrary.util.FileUtils;
import com.github.lazylibrary.util.MD5;
import com.github.lazylibrary.util.ZipUtil;
@ -106,9 +107,14 @@ public class DataSaveUtils {
poiEntity.setUploadMax(splitFiles.size());
sendRefreshItem(poiEntity);
List<Long> chunkSizeList = new ArrayList<>();
long slipeFileSizeCount = 0;
for (File f: splitFiles) {
chunkSizeList.add(f.length());
slipeFileSizeCount+=f.length();
}
XLog.d("各个分包的数据和为:"+slipeFileSizeCount);
XLog.d("zip包的文件大小为"+file.length());
XLog.d("是否一致:"+(Long.compare(slipeFileSizeCount, file.length())));
CommonResponse<String> response = createUploadTask(mContext, auditId, file.length(), chunkSizeList);
if (response!=null) {
// 请求成功获取需要上传的分包index

View File

@ -16,7 +16,6 @@ import java.lang.Exception
object FileSpliteMergeUtils {
const val TAG = "FileUtils"
//默认切割文件的大小
// private const val DEFAULT_CUT_SIZE: Long = 5*1024*1024//5MB
private const val DEFAULT_CUT_SIZE: Long = Constant.DEFAULT_CUT_SIZE//5MB
/**
@ -70,6 +69,32 @@ object FileSpliteMergeUtils {
}
return singleFileList
}
// @JvmStatic
// fun splitFile(file: File, chunkSize: Long = DEFAULT_CUT_SIZE): List<File> {
// val fileSize = file.length()
// val numChunks = (fileSize / chunkSize).toInt() + if (fileSize % chunkSize > 0) 1 else 0
// val chunks = mutableListOf<File>()
//
// RandomAccessFile(file, "r").use { raf ->
// for (i in 0 until numChunks) {
// val chunkFile = File(file.parent, "${file.nameWithoutExtension}_${i + 1}.tmp")
// if (chunkFile.exists()){
// chunkFile.delete()
// }
// RandomAccessFile(chunkFile, "rw").use { chunkRaf ->
// val start = i * chunkSize
// val end = if (i == numChunks - 1) fileSize else (i + 1) * chunkSize
// raf.seek(start)
// while (raf.filePointer < end) {
// chunkRaf.write(raf.read())
// }
// }
// chunks.add(chunkFile)
// }
// }
// return chunks.toList()
// }
private fun createSingleFile(sourceFile: File,index: Long): File{
val path = sourceFile.absolutePath.substringBeforeLast(".")
@ -90,18 +115,18 @@ object FileSpliteMergeUtils {
val out = RandomAccessFile(single,"rw")
var index = 0
try {
val byte = ByteArray(1024)
val byte = ByteArray((end-begin).toInt())
inFile.seek(begin)
while (inFile.read(byte).also { index = it } != -1 && inFile.filePointer <= end){
while (inFile.read(byte).also { index = it } != -1&&inFile.filePointer <= end){
out.write(byte,0,index)
endPointer = inFile.filePointer
}
endPointer = inFile.filePointer
}catch (e: Exception){
e.printStackTrace()
}finally {
out.close()
}
return endPointer - index // 减去最后一次读取的字节数因为while循环中会先读取文件导致filePointer字段向后移动index但是后面的判断条件导致循环跳出所以要将下次读取的offset向前移动
return endPointer
}
/**