Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Unified Diff: third_party/zlib/google/zip_writer.cc

Issue 179963002: New Zip::ZipFromMemory API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressing comments Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/zlib/google/zip_writer.cc
diff --git a/third_party/zlib/google/zip_writer.cc b/third_party/zlib/google/zip_writer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f685359bb9f7960839236dd036bc338da50ec914
--- /dev/null
+++ b/third_party/zlib/google/zip_writer.cc
@@ -0,0 +1,81 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "third_party/zlib/google/zip_writer.h"
+
+#include "base/files/file_path.h"
+#include "base/logging.h"
+#include "base/memory/ref_counted_memory.h"
+#include "third_party/zlib/google/zip.h"
+#include "third_party/zlib/google/zip_internal.h"
+
+namespace zip {
+
+ZipWriter::ZipWriter() {}
+
+void ZipWriter::AddFile(const base::FilePath& file_path,
+ const base::StringPiece& contents) {
+ DCHECK(contents.data());
+ CheckValidFilePath(file_path);
+ contents_[file_path] = contents;
+}
+
+void ZipWriter::AddFile(const base::FilePath& file_path,
+ const base::RefCountedMemory* contents) {
+ DCHECK(contents);
+ AddFile(file_path,
+ base::StringPiece(contents->front_as<char>(), contents->size()));
+}
+
+void ZipWriter::AddFolder(const base::FilePath& folder_path) {
+ CheckValidFolderPath(folder_path);
+ contents_[folder_path.AsEndingWithSeparator()] = base::StringPiece("", 0);
+}
+
+void ZipWriter::DeleteFile(const base::FilePath& file_path) {
+ CheckValidFilePath(file_path);
+ contents_[file_path] = base::StringPiece();
+}
+
+void ZipWriter::DeleteFolder(const base::FilePath& folder_path) {
+ CheckValidFolderPath(folder_path);
+ contents_[folder_path.AsEndingWithSeparator()] = base::StringPiece();
+}
+
+void ZipWriter::DropModifications(const base::FilePath& file_or_folder_path) {
+ ZipContents::iterator position = contents_.find(file_or_folder_path);
+ if (position == contents_.end())
+ position = contents_.find(file_or_folder_path.EndsWithSeparator() ?
+ file_or_folder_path.StripTrailingSeparators() :
+ file_or_folder_path.AsEndingWithSeparator());
+ if (position != contents_.end())
+ contents_.erase(position);
+}
+
+bool ZipWriter::Commit(const base::FilePath& zip_file, CommitMode mode) const {
+ return internal::ZipFromMemory(zip_file, contents_, mode == Append);
+}
+
+void ZipWriter::Clear() {
+ contents_.clear();
+}
+
+bool ZipWriter::HasFile(const base::FilePath& file_path) const {
+ return contents_.find(file_path.StripTrailingSeparators()) != contents_.end();
+}
+bool ZipWriter::HasFolder(const base::FilePath& folder_path) const {
+ return contents_.find(folder_path.AsEndingWithSeparator()) != contents_.end();
+}
+
+void ZipWriter::CheckValidFilePath(const base::FilePath& path) const {
+ DCHECK(!path.empty() && !path.IsAbsolute() && !path.ReferencesParent());
+ DCHECK(!path.EndsWithSeparator());
+ DCHECK(!HasFolder(path));
+}
+void ZipWriter::CheckValidFolderPath(const base::FilePath& path) const {
+ DCHECK(!path.empty() && !path.IsAbsolute() && !path.ReferencesParent());
+ DCHECK(!HasFile(path));
+}
+
+} // namespace zip

Powered by Google App Engine
This is Rietveld 408576698