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 |