Skip to content

Commit

Permalink
add write to gzip compressed file function
Browse files Browse the repository at this point in the history
  • Loading branch information
omer-candan committed Aug 21, 2024
1 parent 9b8aa84 commit 8f3e844
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ortools/base/gzipfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,51 @@ File* GZipFileReader(const absl::string_view name, File* file,
LOG(INFO) << "not implemented";
return nullptr;
}

absl::Status WriteToGzipFile(const absl::string_view filename,
const absl::string_view contents) {
// 1 for fastest compression.
gzFile gz_file = gzopen(filename.data(), "w1");
if (gz_file == Z_NULL) {
return {absl::StatusCode::kInternal, "Unable to open file"};
}

// have to write in chunks, otherwise fails to compress big files.
constexpr size_t chunk = 1024 * 1024; // 1 MB chunk size.
size_t remaining = contents.size();
const char* dataPtr = contents.data();

while (remaining > 0) {
size_t current_chunk = std::min(chunk, remaining);
size_t written = gzwrite(gz_file, dataPtr, current_chunk);
if (written != current_chunk) {
int errNo = 0;
absl::string_view error_message = gzerror(gz_file, &errNo);
gzclose(gz_file);
return {
absl::StatusCode::kInternal,
absl::StrCat(
"Error while writing chunk to compressed file:",
error_message
)
};
}

dataPtr += current_chunk;
remaining -= current_chunk;
}

if (const int status = gzclose(gz_file); status != Z_OK) {
int errNo;
absl::string_view error_message = gzerror(gz_file, &errNo);
return {
absl::StatusCode::kInternal,
absl::StrCat(
"Error while writing chunk to compressed file:",
error_message
)
};
}

return absl::OkStatus();
}
6 changes: 6 additions & 0 deletions ortools/base/gzipfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef OR_TOOLS_BASE_GZIPFILE_H_
#define OR_TOOLS_BASE_GZIPFILE_H_

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "ortools/base/basictypes.h" // for Ownership enum
#include "zlib.h" // for Z_DEFAULT_COMPRESSION
Expand Down Expand Up @@ -65,4 +66,9 @@ inline File* GZipFileReader(absl::string_view name, File* compressed_file,
AppendedStreams::kConcatenateStreams);
}

// Write contents into a gzip compressed file with the lowest compression level
// which is the fastest.
absl::Status WriteToGzipFile(absl::string_view filename,
absl::string_view contents);

#endif // OR_TOOLS_BASE_GZIPFILE_H_

0 comments on commit 8f3e844

Please sign in to comment.