MCPcopy Create free account
hub / github.com/comaps/comaps / CreateZipFromFiles

Function CreateZipFromFiles

libs/coding/zip_creator.cpp:62–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60}
61
62bool CreateZipFromFiles(std::vector<std::string> const & files, std::vector<std::string> const & filesInArchive,
63 std::string const & zipFilePath, CompressionLevel compression)
64{
65 ASSERT_EQUAL(files.size(), filesInArchive.size(), ("List of file names in archive doesn't match list of files"));
66 SCOPE_GUARD(outFileGuard, [&zipFilePath]() { base::DeleteFileX(zipFilePath); });
67
68 ZipHandle zip(zipFilePath);
69 if (!zip.Handle())
70 return false;
71
72 auto const compressionLevel = GetCompressionLevel(compression);
73
74 try
75 {
76 int suffix = 0;
77 for (size_t i = 0; i < files.size(); i++)
78 {
79 auto const & filePath = files.at(i);
80 std::string fileInArchive = filesInArchive.at(i);
81 if (!strings::IsASCIIString(fileInArchive))
82 {
83 if (suffix == 0)
84 fileInArchive = "CoMaps.kml";
85 else
86 fileInArchive = "CoMaps_" + std::to_string(suffix) + ".kml";
87 ++suffix;
88 }
89 zip::FileInfo fileInfo = {};
90 FillZipLocalDateTime(fileInfo.tmz_date);
91 if (zip::Code::Ok !=
92 zip::OpenNewFileInZip(zip.Handle(), fileInArchive, fileInfo, "", Z_DEFLATED, compressionLevel))
93 return false;
94
95 base::FileData file(filePath, base::FileData::Op::READ);
96 uint64_t const fileSize = file.Size();
97 uint64_t writtenSize = 0;
98 std::array<char, zip::kFileBufferSize> buffer;
99
100 while (writtenSize < fileSize)
101 {
102 auto const filePartSize = std::min(buffer.size(), static_cast<size_t>(fileSize - writtenSize));
103 file.Read(writtenSize, buffer.data(), filePartSize);
104 if (zip::Code::Ok != zip::WriteInFileInZip(zip.Handle(), buffer, filePartSize))
105 return false;
106 writtenSize += filePartSize;
107 }
108 zipCloseFileInZip(zip.Handle());
109 }
110 }
111 catch (std::exception const & e)
112 {
113 LOG(LERROR, ("Error adding files to the archive", zipFilePath, e.what()));
114 return false;
115 }
116
117 outFileGuard.release();
118 return true;
119}

Callers 6

UNIT_TESTFunction · 0.85
CreateAndTestZipFunction · 0.85
ExportSingleFileKmlFunction · 0.85
ExportMultipleFilesFunction · 0.85
PrepareArchivesMethod · 0.85

Calls 15

DeleteFileXFunction · 0.85
GetCompressionLevelFunction · 0.85
IsASCIIStringFunction · 0.85
to_stringFunction · 0.85
FillZipLocalDateTimeFunction · 0.85
OpenNewFileInZipFunction · 0.85
WriteInFileInZipFunction · 0.85
zipCloseFileInZipFunction · 0.85
FileNameFromFullPathFunction · 0.85
atMethod · 0.80
sizeMethod · 0.45
HandleMethod · 0.45

Tested by 3

UNIT_TESTFunction · 0.68
CreateAndTestZipFunction · 0.68