in c++, I used this code to compress a file, and then extracted it manually and the file…
- User
in c++, I used this code to compress a file, and then extracted it manually and the file sizes doesn't match, is something wrong with my code? Maybe it has to do with the encoding?
void CompressionUtils::CompressFile(const wxString& filePath, const wxString& zipFilePath) { try { wxMemoryBuffer buffer = FileUtils::ReadFileToBuffer(filePath);
wxString zipPath = zipFilePath; wxFileName fileName(filePath); if (zipFilePath.IsEmpty()) { wxFileName zipFileName(filePath); zipFileName.SetExt("zip"); zipPath = zipFileName.GetFullPath(); } wxFileOutputStream fileOut(zipPath); if (!fileOut.IsOk()) { throw std::runtime_error("Error opening zip file for writing: " + zipPath.ToStdString()); } wxZipOutputStream zipOut(fileOut); zipOut.PutNextEntry(fileName.GetFullName()); if (!zipOut.WriteAll(buffer.GetData(), buffer.GetBufSize())) { throw std::runtime_error("Error writing data to zip: " + zipPath.ToStdString()); } zipOut.CloseEntry(); } catch (...) { throw std::runtime_error("Error opening file: " + filePath.ToStdString()); }}
Original File Size: 26.8 KiB Extracted File Size: 27.8 KiB
- Assistant
- User
I tried to open the extracted file and it says it is not a valid UTF-8 document:
The document is not UTF-8 valid. Other partially valid encodings were found, please choose below.
- Assistant
- User
wxMemoryBuffer ReadFileToBuffer(const wxString& filePath) { if (CheckFileExists(filePath)) { wxFile file(filePath); if (!file.IsOpened()) { throw std::runtime_error("Error reading file: " + filePath.ToStdString()); }
wxFileOffset fileSize = file.Length(); wxMemoryBuffer buffer; file.Read(buffer.GetWriteBuf(fileSize), fileSize); buffer.UngetWriteBuf(fileSize); return buffer; } else { throw std::runtime_error("Error file does not exists: " + filePath.ToStdString()); } } - Assistant
- User
I changed the reading of the memory buffer and it doesn't give me any errors like wrong size, so I will assume this is correct.
Now, I checked the file using a diff util, and both files are the same except the end part. In the correct one it ends up in "}" but the extracted one goes after this character adding invisible characters that I can't copy here
- Assistant
- User
I printed the buffer data like this:
for (size_t i = buffer.GetBufSize() - inspectSize; i < buffer.GetBufSize(); ++i) { std::cout << "0x%02X " << data[i]; std::cout << "\n"; } std::cout << "\n";
and got empty lines: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X
I also added the closing of all the files but didn't changed anything.
I checked the hex data of the extracted file, and after the original ending, it is all "00"
- Assistant
- User
ok, now with the last code it throws the exception written size does not match buffer size
- Assistant
- User
Buffer Size: 28440, Original File Size: 27416 Bytes Written: 1
- Assistant
- User
I changed the buffer readind as you suggested and gave me the same error
- Assistant
- User
Original File Size: 27416 Bytes Read: 27416 Buffer Size: 28440, Original File Size: 27416 Bytes Written: 1
- Assistant
- User
wait, I am not using SetDataSize but SetBufSize, maybe that is where the problem lies?
This is the documentation of the methods:
SetBufSize() void wxMemoryBuffer::SetBufSize ( size_t size )
Ensures the buffer has at least size bytes available. SetDataLen() void wxMemoryBuffer::SetDataLen ( size_t size )
Sets the length of the data stored in the buffer.
Mainly useful for truncating existing data.
Parameters size New length of the valid data in the buffer. This is distinct from the allocated size
UngetWriteBuf() void wxMemoryBuffer::UngetWriteBuf ( size_t sizeUsed )
Update the buffer after completing a direct write, which you must have used GetWriteBuf() to initialise.
Parameters sizeUsed The amount of data written in to buffer by the direct write
- Assistant
- User
it gave me the same error, even thought we set the data len.
Original File Size: 27416 Bytes Read: 27416 Buffer Size: 28440, Original File Size: 27416 Bytes Written: 1
- Assistant
- User
yes the buffer size doesn't change its size even though the data len does:
Original File Size: 27416 Bytes Read: 27416 Buffer Size After Read: 28440, Data Length After Read: 27416 Buffer Size: 28440, Original File Size: 27416 Bytes Written: 1
- Assistant
- User
perfect! now we have a working solution, now how to decompress the zip we just created overwriting the files if needed?
- Assistant
- User
OK got some code but it is not writing anything nor creating any file:
void CompressionUtils::DecompressFile(const wxString& zipFilePath) { try { wxMemoryBuffer buffer = FileUtils::ReadFileToBuffer(zipFilePath);
wxFileInputStream fileIn(zipFilePath); if (!fileIn.ReadAll(buffer.GetData(), buffer.GetDataLen())) { throw std::runtime_error("Error reading data from zip: " + zipFilePath.ToStdString()); } wxZipInputStream zipIn(fileIn); wxZipEntry* entry; while ((entry = zipIn.GetNextEntry()) != nullptr) { wxString entryName = entry->GetName(); wxFileOutputStream output(entryName); if (!output.IsOk()) { throw std::runtime_error("Error creating output file: " + entryName.ToStdString()); } zipIn.Read(output); output.Close(); zipIn.CloseEntry(); } zipIn.CloseEntry(); } catch (...) { throw std::runtime_error("Error opening file: " + zipFilePath.ToStdString()); }}
- Assistant
- User
no I won't be compressing directories, just files, and they will be extracted in the same directory as original zip file
- Assistant