I have these two c++ methods that are mostly similar except in the end part where it…
- User
I have these two c++ methods that are mostly similar except in the end part where it chooses to return a wxstring vs save to a file. Can we combine both into a single method, like always returning a wxString and saving to a file if there is a flag?
wxString CompressionUtils::Decompress(const wxString& zipFilePath) { wxString decompressedString;
try { wxFileInputStream fileIn(zipFilePath); if (!fileIn.IsOk()) { throw std::runtime_error("Error opening zip file: " + zipFilePath.ToStdString()); } wxZipInputStream zipIn(fileIn); wxZipEntry* entry = nullptr; while ((entry = zipIn.GetNextEntry()) != nullptr) { if (entry->IsDir()) { zipIn.CloseEntry(); continue; } wxStringOutputStream output; zipIn.Read(output); if (zipIn.LastRead() == 0) { throw std::runtime_error("Error reading entry from zip file: " + entry->GetName().ToStdString()); } decompressedString += output.GetString(); zipIn.CloseEntry(); output.Close(); } } catch (const std::exception& e) { throw std::runtime_error("Error decompressing file to string: " + std::string(e.what())); } return decompressedString;}
void CompressionUtils::DecompressFile(const wxString& zipFilePath) { try { wxFileName zipFileName(zipFilePath); wxString outputDirectory = zipFileName.GetPath();
wxFileInputStream fileIn(zipFilePath); if (!fileIn.IsOk()) { throw std::runtime_error("Error opening zip file: " + zipFilePath.ToStdString()); } wxZipInputStream zipIn(fileIn); wxZipEntry* entry = nullptr; while ((entry = zipIn.GetNextEntry()) != nullptr) { wxString outputPath = outputDirectory + "/" + entry->GetName(); if (entry->IsDir()) { zipIn.CloseEntry(); continue; } wxFileOutputStream output(outputPath); if (!output.IsOk()) { throw std::runtime_error("Error creating output file: " + outputPath.ToStdString()); } zipIn.Read(output); if (zipIn.LastRead() == 0) { throw std::runtime_error("Error writing output from zip file: " + outputPath.ToStdString()); } output.Close(); zipIn.CloseEntry(); } } catch (const std::exception& e) { throw std::runtime_error("Error decompressing file: " + std::string(e.what())); }}
- Assistant
- User
it works, but then I wanted to do them to always return the read string and now it is giving me errors, probablñy because the input is read twice and the position of the cursor has moved, how can I do this without breaking?
wxString CompressionUtils::DecompressFile(const wxString& zipFilePath, bool saveToFile) { wxString decompressedString;
try { wxFileName zipFileName(zipFilePath); wxFileInputStream fileIn(zipFilePath); if (!fileIn.IsOk()) { throw std::runtime_error("Error opening zip file: " + zipFilePath.ToStdString()); } wxZipInputStream zipIn(fileIn); wxZipEntry* entry = nullptr; while ((entry = zipIn.GetNextEntry()) != nullptr) { wxString outputPath = zipFileName.GetPath() + "/" + entry->GetName(); if (entry->IsDir()) { zipIn.CloseEntry(); continue; } wxStringOutputStream output; zipIn.Read(output); decompressedString += output.GetString(); if (saveToFile) { wxFileOutputStream output(outputPath); if (!output.IsOk()) { throw std::runtime_error("Error creating output file: " + outputPath.ToStdString()); } zipIn.Read(output); output.Close(); } if (zipIn.LastRead() == 0) { throw std::runtime_error("Error reading entry from zip file: " + outputPath.ToStdString()); } zipIn.CloseEntry(); } } catch (const std::exception& e) { throw std::runtime_error("Error decompressing file: " + std::string(e.what())); } return decompressedString; - Assistant
- User
no it doesn't work, it gave me many errors, do a different buffer type
- Assistant
- User
I wanted to do something similar for the compression but I guess it is different since you can compress a file or a wxstring, and now I want to compress the wxstring, so I think those two can't be combined as the last two, right? Help me compress a wxString and save to a file:
void CompressionUtils::CompressFile(const wxString& filePath, const wxString& zipFilePath) { try { wxMemoryBuffer buffer = FileUtils::ReadFileToBuffer(filePath);
wxString zipPath = zipFilePath; 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()); } wxFileName fileName(filePath); wxZipOutputStream zipOut(fileOut); zipOut.PutNextEntry(fileName.GetFullName()); zipOut.WriteAll(buffer.GetData(), buffer.GetDataLen()); zipOut.CloseEntry(); zipOut.Close(); fileOut.Close(); } catch (const std::exception& e) { throw std::runtime_error("Error processing file: " + filePath.ToStdString() + ". " + e.what()); }}
- Assistant