static bool ReadDataString(LPCWSTR fileName, LPCSTR startID, LPCSTR endID, AString &stringResult) { stringResult.Empty(); NFile::NIO::CInFile inFile; if (!inFile.Open(fileName)) return false; const int kBufferSize = (1 << 12); Byte buffer[kBufferSize]; int signatureStartSize = lstrlenA(startID); int signatureEndSize = lstrlenA(endID); UInt32 numBytesPrev = 0; bool writeMode = false; UInt64 posTotal = 0; for (;;) { if (posTotal > (1 << 20)) return (stringResult.IsEmpty()); UInt32 numReadBytes = kBufferSize - numBytesPrev; UInt32 processedSize; if (!inFile.Read(buffer + numBytesPrev, numReadBytes, processedSize)) return false; if (processedSize == 0) return true; UInt32 numBytesInBuffer = numBytesPrev + processedSize; UInt32 pos = 0; for (;;) { if (writeMode) { if (pos > numBytesInBuffer - signatureEndSize) break; if (memcmp(buffer + pos, endID, signatureEndSize) == 0) return true; char b = buffer[pos]; if (b == 0) return false; stringResult += b; pos++; } else { if (pos > numBytesInBuffer - signatureStartSize) break; if (memcmp(buffer + pos, startID, signatureStartSize) == 0) { writeMode = true; pos += signatureStartSize; } else pos++; } } numBytesPrev = numBytesInBuffer - pos; posTotal += pos; memmove(buffer, buffer + pos, numBytesPrev); } }
HRESULT CFSDrives::GetLength(int index, UInt64 &length) const { NFile::NIO::CInFile inFile; if (!inFile.Open(_drives[index].GetDeviceFileIoName())) return GetLastError(); if (!inFile.LengthDefined) return E_FAIL; length = inFile.Length; return S_OK; }
HRESULT CThreadCombine::ProcessVirt() { NFile::NIO::COutFile outFile; if (!outFile.Create(OutputPath, false)) { HRESULT res = GetLastError(); ErrorPath1 = OutputPath; return res; } CProgressSync &sync = ProgressDialog.Sync; sync.SetProgress(TotalSize, 0); CMyBuffer bufferObject; if (!bufferObject.Allocate(kBufSize)) return E_OUTOFMEMORY; Byte *buffer = (Byte *)(void *)bufferObject; UInt64 pos = 0; for (int i = 0; i < Names.Size(); i++) { NFile::NIO::CInFile inFile; const UString nextName = InputDirPrefix + Names[i]; if (!inFile.Open(nextName)) { HRESULT res = GetLastError(); ErrorPath1 = nextName; return res; } sync.SetCurrentFileName(nextName); for (;;) { UInt32 processedSize; if (!inFile.Read(buffer, kBufSize, processedSize)) { HRESULT res = GetLastError(); ErrorPath1 = nextName; return res; } if (processedSize == 0) break; UInt32 needSize = processedSize; if (!outFile.Write(buffer, needSize, processedSize)) { HRESULT res = GetLastError(); ErrorPath1 = OutputPath; return res; } if (needSize != processedSize) throw g_Message_FileWriteError; pos += processedSize; RINOK(sync.SetPosAndCheckPaused(pos)); } } return S_OK; }
static HRESULT CopyFileSpec(LPCWSTR fromPath, LPCWSTR toPath, bool writeToDisk, UInt64 fileSize, UInt32 bufferSize, UInt64 progressStart, IProgress *progress) { NFile::NIO::CInFile inFile; if (!inFile.Open(fromPath)) return GetLastError(); if (fileSize == (UInt64)(Int64)-1) { if (!inFile.GetLength(fileSize)) ::GetLastError(); } NFile::NIO::COutFile outFile; if (writeToDisk) { if (!outFile.Open(toPath, FILE_SHARE_WRITE, OPEN_EXISTING, 0)) return GetLastError(); } else if (!outFile.Create(toPath, true)) return GetLastError(); CPhysTempBuffer tempBuffer; tempBuffer.buffer = MidAlloc(bufferSize); if (tempBuffer.buffer == 0) return E_OUTOFMEMORY; for (UInt64 pos = 0; pos < fileSize;) { UInt64 progressCur = progressStart + pos; RINOK(progress->SetCompleted(&progressCur)); UInt64 rem = fileSize - pos; UInt32 curSize = (UInt32)MyMin(rem, (UInt64)bufferSize); UInt32 processedSize; if (!inFile.Read(tempBuffer.buffer, curSize, processedSize)) return GetLastError(); if (processedSize == 0) break; curSize = processedSize; if (writeToDisk) { const UInt32 kMask = 0x1FF; curSize = (curSize + kMask) & ~kMask; if (curSize > bufferSize) return E_FAIL; } if (!outFile.Write(tempBuffer.buffer, curSize, processedSize)) return GetLastError(); if (curSize != processedSize) return E_FAIL; pos += curSize; } return S_OK; }
HRESULT CThreadSplit::ProcessVirt() { NFile::NIO::CInFile inFile; if (!inFile.Open(FilePath)) return GetLastError(); NFile::NIO::COutFile outFile; CMyBuffer bufferObject; if (!bufferObject.Allocate(kBufSize)) return E_OUTOFMEMORY; Byte *buffer = (Byte *)(void *)bufferObject; UInt64 curVolSize = 0; CVolSeqName seqName; seqName.SetNumDigits(NumVolumes); UInt64 length; if (!inFile.GetLength(length)) return GetLastError(); CProgressSync &sync = ProgressDialog.Sync; sync.SetProgress(length, 0); UInt64 pos = 0; UInt64 numFiles = 0; int volIndex = 0; for (;;) { UInt64 volSize; if (volIndex < VolumeSizes.Size()) volSize = VolumeSizes[volIndex]; else volSize = VolumeSizes.Back(); UInt32 needSize = (UInt32)(MyMin((UInt64)kBufSize, volSize - curVolSize)); UInt32 processedSize; if (!inFile.Read(buffer, needSize, processedSize)) return GetLastError(); if (processedSize == 0) break; needSize = processedSize; if (curVolSize == 0) { UString name = VolBasePath; name += L'.'; name += seqName.GetNextName(); sync.SetCurrentFileName(name); sync.SetNumFilesCur(numFiles++); if (!outFile.Create(name, false)) { HRESULT res = GetLastError(); ErrorPath1 = name; return res; } } if (!outFile.Write(buffer, needSize, processedSize)) return GetLastError(); if (needSize != processedSize) throw g_Message_FileWriteError; curVolSize += processedSize; if (curVolSize == volSize) { outFile.Close(); if (volIndex < VolumeSizes.Size()) volIndex++; curVolSize = 0; } pos += processedSize; RINOK(sync.SetPosAndCheckPaused(pos)); } sync.SetNumFilesCur(numFiles); return S_OK; }
void Process2() { // NCOM::CComInitializer comInitializer; ProgressDialog->WaitCreating(); CVolSeqName volSeqName; if (!volSeqName.ParseName(FirstVolumeName)) throw L"Can not detect file as splitted file"; UString nextName = InputDirPrefix + FirstVolumeName; UInt64 totalSize = 0; for (;;) { NFile::NFind::CFileInfoW fileInfo; if (!NFile::NFind::FindFile(nextName, fileInfo)) break; if (fileInfo.IsDirectory()) break; totalSize += fileInfo.Size; nextName = InputDirPrefix + volSeqName.GetNextName(); } if (totalSize == 0) throw L"no data"; ProgressDialog->ProgressSynch.SetProgress(totalSize, 0); if (!volSeqName.ParseName(FirstVolumeName)) throw L"Can not detect file as splitted file"; UString outName = volSeqName.UnchangedPart; while(!outName.IsEmpty()) { int lastIndex = outName.Length() - 1; if (outName[lastIndex] != L'.') break; outName.Delete(lastIndex); } if (outName.IsEmpty()) outName = L"file"; NFile::NIO::COutFile outFile; if (!outFile.Create(OutputDirPrefix + outName, false)) throw L"Can create open output file"; NFile::NIO::CInFile inFile; CMyBuffer bufferObject; if (!bufferObject.Allocate(kBufSize)) throw L"Can not allocate buffer"; Byte *buffer = (Byte *)(void *)bufferObject; UInt64 pos = 0; nextName = InputDirPrefix + FirstVolumeName; bool needOpen = true; for (;;) { if (needOpen) { NFile::NFind::CFileInfoW fileInfo; if (!NFile::NFind::FindFile(nextName, fileInfo)) break; if (fileInfo.IsDirectory()) break; if (!inFile.Open(nextName)) throw L"Can not open file"; ProgressDialog->ProgressSynch.SetCurrentFileName(fileInfo.Name); nextName = InputDirPrefix + volSeqName.GetNextName(); needOpen = false; } UInt32 processedSize; if (!inFile.Read(buffer, kBufSize, processedSize)) throw L"Can not read input file"; if (processedSize == 0) { needOpen = true; continue; } UInt32 needSize = processedSize; if (!outFile.Write(buffer, needSize, processedSize)) throw L"Can not write output file"; if (needSize != processedSize) throw L"Can not write output file"; pos += processedSize; HRESULT res = ProgressDialog->ProgressSynch.SetPosAndCheckPaused(pos); if (res != S_OK) return; } }
void Process2() { // NCOM::CComInitializer comInitializer; ProgressDialog->WaitCreating(); NFile::NIO::CInFile inFile; if (!inFile.Open(FilePath)) throw L"Can not open file"; NFile::NIO::COutFile outFile; CMyBuffer bufferObject; if (!bufferObject.Allocate(kBufSize)) throw L"Can not allocate buffer"; Byte *buffer = (Byte *)(void *)bufferObject; UInt64 curVolSize = 0; CVolSeqName seqName; UInt64 length; if (!inFile.GetLength(length)) throw "error"; ProgressDialog->ProgressSynch.SetProgress(length, 0); UInt64 pos = 0; int volIndex = 0; for (;;) { UInt64 volSize; if (volIndex < VolumeSizes.Size()) volSize = VolumeSizes[volIndex]; else volSize = VolumeSizes.Back(); UInt32 needSize = (UInt32)(MyMin((UInt64)kBufSize, volSize - curVolSize)); UInt32 processedSize; if (!inFile.Read(buffer, needSize, processedSize)) throw L"Can not read input file"; if (processedSize == 0) break; needSize = processedSize; if (curVolSize == 0) { UString name = VolBasePath; name += L"."; name += seqName.GetNextName(); if (!outFile.Create(name, false)) throw L"Can not create output file"; ProgressDialog->ProgressSynch.SetCurrentFileName(name); } if (!outFile.Write(buffer, needSize, processedSize)) throw L"Can not write output file"; if (needSize != processedSize) throw L"Can not write output file"; curVolSize += processedSize; if (curVolSize == volSize) { outFile.Close(); if (volIndex < VolumeSizes.Size()) volIndex++; curVolSize = 0; } pos += processedSize; HRESULT res = ProgressDialog->ProgressSynch.SetPosAndCheckPaused(pos); if (res != S_OK) return; } }