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

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

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.h
diff --git a/third_party/zlib/google/zip_writer.h b/third_party/zlib/google/zip_writer.h
new file mode 100644
index 0000000000000000000000000000000000000000..d2002c55ef16ed85afae561165052effb5ddf32e
--- /dev/null
+++ b/third_party/zlib/google/zip_writer.h
@@ -0,0 +1,116 @@
+// Copyright (c) 2011 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.
+#ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_
+#define THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_
+
+#include <map>
+
+#include "base/strings/string_piece.h"
+
+namespace base {
+class FilePath;
+class RefCountedMemory;
+}
+
+namespace zip {
+
+// This class is used for creating or modifying zip files.
+//
+// ZipWriter writer;
+// writer.AddFile(<path>,<contents>);
+// writer.DeleteFile(<path>);
+// writer.AddFolder(<path>);
+// writer.DeleteFolder(<path>);
+// writer.Commit(<zip path>,Append/OverWrite);
+//
+// All modifications are batched and executed in one shot during the call to
+// Commit. Commit() is the only method that does file IO. The target zip file
+// to modify will be rewritten from scratch, if contents need to be modified
+// or removed.
+//
+// Many calls to Add{File,Folder}/Delete{File,Folder} with the same path will
+// override the previous ones, so only the last modification applies.
+//
+// IMPORTANT: ZipWriter does not copy nor take ownership of any of the passed
+// file contents. It's the responsibility of the caller to ensure any pointer
+// passed is valid while the ZipWriter object is kept alive, or until Clear()
+// is called.
+//
+class ZipWriter {
+ public:
+ enum CommitMode {
+ // Overwrites any existing file
+ OverWrite,
+ // Appends to existing file
+ Append
+ };
+
+ typedef std::map<const base::FilePath, base::StringPiece> ZipContents;
+ typedef ZipContents::const_iterator const_iterator;
+
+ ZipWriter();
+
+ // Adds a new file to the archive, or replaces if it already exists.
+ // |file_path| is the relative path used to name the file in the archive.
+ // |contents| is the contents of the new file.
+ void AddFile(const base::FilePath& file_path,
+ const base::StringPiece& contents);
+ void AddFile(const base::FilePath& file_path,
+ const base::RefCountedMemory* contents);
+
+ // Adds a new folder to the archive, or replaces if it already exists.
+ // |folder_path| is the relative path used to name the folder in the archive.
+ void AddFolder(const base::FilePath& folder_path);
+
+ // Marks the file with the given |file_path| to be deleted from the archive.
+ void DeleteFile(const base::FilePath& file_path);
+
+ // Marks the file with the given |folder_path| to be deleted from the archive.
+ void DeleteFolder(const base::FilePath& folder_path);
+
+ // Drops any Adds or Deletes for the given |file_or_folder_path| so further
+ // calls to Commit() will not modify that file or folder.
+ void DropModifications(const base::FilePath& file_or_folder_path);
+
+ // Commits all changes to |zip_file|. The file will be created from scratch
+ // or appended regarding the value or |mode|. This function does file IO.
+ bool Commit(const base::FilePath& zip_file, CommitMode mode) const;
+
+ // Clears all pending changes, making this ZipWriter empty. Any pointer kept
+ // internally passed to AddFile is dropped. Calling Commit() with an empty
+ // ZipWriter has no effect.
+ void Clear();
+
+ const_iterator begin() const {
+ return contents_.begin();
+ }
+
+ const_iterator end() const {
+ return contents_.end();
+ }
+
+ private:
+#if __cplusplus >= 201103L
+ // Trick to prevent temporary strings being passed to AddFile, because
+ // ZipWriter does not copy the data, so deleted data would be used later.
+ // This just causes a compilation failure.
+ template<int x = 0>
+ void AddFile(const base::FilePath&, const std::string&&);
+#endif
+
+ bool HasFile(const base::FilePath& file_path) const;
+ bool HasFolder(const base::FilePath& folder_path) const;
+
+ void CheckValidFilePath(const base::FilePath& folder_path) const;
+ void CheckValidFolderPath(const base::FilePath& folder_path) const;
+
+ // Set of files to add or delete.
+ ZipContents contents_;
+
+ DISALLOW_COPY_AND_ASSIGN(ZipWriter);
+};
+
+} // namespace zip
+
+#endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_

Powered by Google App Engine
This is Rietveld 408576698