private static long GetFileSize(string path) { if (!XLPack.IsFileExist(path)) { return(-1); } long s = 0; var position = XLPack.FOpen(path, "r"); s = XLPack.FSize(position); XLPack.FClose(ref position); return(s); }
private static bool ReCalculateFileMD5(string path) { if (!XLPack.IsFileExist(path)) { return(false); } var position = XLPack.FOpen(path, "r"); long fileSize = XLPack.FSize(position); const int bufSize = 0x4000; byte[] buffer = new byte[bufSize]; IntPtr bufPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); // TODO: Do this without reading the entire file into memory to calculate the MD5 // Maybe try to use the XLPack.DLL's MD5Init, MD5Update and MD5 Finalize functions ? // Using ChunkedMemoryStream instead of MemoryStream to hopefully avoid outofmemory errors on large files ChunkedMemoryStream ms = new ChunkedMemoryStream(); long readTotalSize = 0; while (readTotalSize < fileSize) { long readSize = fileSize - readTotalSize; if (readSize > bufSize) { readSize = bufSize; } XLPack.FRead(position, bufPtr, readSize); ms.Write(buffer, 0, (int)readSize); // readSize should never be out of int range, so it's safe to cast it readTotalSize += readSize; } XLPack.FClose(ref position); ms.Position = 0; MD5 md5Hash = MD5.Create(); string md5String = GetMd5Hash(md5Hash, ms).Replace("-", "").ToLower(); ms.Dispose(); var res = SetFileMD5(path, md5String); return(res); }