Chromium Code Reviews| Index: chrome/common/zip_reader.h |
| diff --git a/chrome/common/zip_reader.h b/chrome/common/zip_reader.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4083c10115c70ae4290bfc00cb6f8475171a2e0f |
| --- /dev/null |
| +++ b/chrome/common/zip_reader.h |
| @@ -0,0 +1,143 @@ |
| +// 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 CHROME_COMMON_ZIP_READER_H_ |
| +#define CHROME_COMMON_ZIP_READER_H_ |
| +#pragma once |
| + |
| +#include <string> |
| + |
| +#include "base/file_path.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/time.h" |
| +#include "third_party/zlib/contrib/minizip/unzip.h" |
| + |
| +namespace zip { |
| + |
| +// This class is used for reading zip files. A typical use case of this |
| +// class is to scan files in a zip file and extract them. The code will |
| +// look like: |
| +// |
| +// ZipReader reader; |
| +// reader.Open(zip_file_path); |
| +// while (reader.HasMore()) { |
| +// reader.OpenCurrentFileInZip(); |
| +// reader.ExtractCurrentFileToDirectory(output_directory_path); |
| +// reader.AdvanceToNextFile(); |
| +// } |
| +// |
| +// For simplicty, error checking is omitted in the example code above. The |
| +// production code should check return values from all of these functions. |
| +// |
| +// This calls can also be used for random access of contents in a zip file |
| +// using LocateAndOpenFile(). |
| +// |
| +class ZipReader { |
|
Aaron Boodman
2011/11/09 08:00:40
Nice API.
satorux1
2011/11/10 08:00:56
Thanks!
|
| + public: |
| + // This class represents information of a file in a zip file. |
| + class FileInfo { |
| + public: |
| + FileInfo(const std::string& filename_in_zip, |
| + const unz_file_info& raw_file_info); |
| + |
| + // Returns the file path. |
|
Aaron Boodman
2011/11/09 08:00:40
It would be nice to indicate whether they are pref
satorux1
2011/11/10 08:00:56
Done. This comment made me to add a new evil zip f
|
| + const FilePath& file_path() const { return file_path_; } |
| + |
| + // Returns the size of the original file (i.e. after uncompressed). |
| + const int64 original_size() const { return original_size_; } |
| + |
| + // Returns the last modified time. |
| + const base::Time last_modified() const { return last_modified_; } |
| + |
| + // Returns true if the file is a directory. |
| + bool is_directory() const { return is_directory_; } |
| + |
| + // Returns true if the file is unsafe, like having ".." in its file |
| + // name or invalid UTF-8 characters. |
| + bool is_unsafe() const { return is_unsafe_; } |
| + |
| + private: |
| + const FilePath file_path_; |
| + int64 original_size_; |
| + base::Time last_modified_; |
| + bool is_directory_; |
| + bool is_unsafe_; |
| + DISALLOW_COPY_AND_ASSIGN(FileInfo); |
| + }; |
| + |
| + ZipReader(); |
| + ~ZipReader(); |
| + |
| + // Opens the zip file specified by |zip_file_path|. Returns true on |
| + // success. |
| + bool Open(const FilePath& zip_file_path); |
| + |
| + // Closes the currently opened zip file. This function is called in the |
| + // destructor of the class, so you usually don't need to call this. |
| + void Close(); |
| + |
| + // Returns true if there is at least one file to read. This function is |
| + // used to scan files with AdvanceToNextFile(), like: |
| + // |
| + // while (reader.HasMore()) { |
| + // // Do something with the current file here. |
| + // reader.AdvanceToNextFile(); |
| + // } |
| + bool HasMore(); |
| + |
| + // Advances the next file. Returns true on success. |
| + bool AdvanceToNextFile(); |
| + |
| + // Opens the current file in the zip file. On success, returns true and |
| + // updates the the current file state (i.e. current_file_info() is |
| + // updated). This function should be called before operations over the |
| + // current file like ExtractCurrentFileToFile(). |
| + // |
| + // Note that there is no CloseCurrentFileInZip(). The the current file |
| + // state is reset automatically as needed. |
| + bool OpenCurrentFileInZip(); |
| + |
| + // Locates a file in the zip file and opens it. Returns true on success. |
| + // This function internally calls OpenCurrentFileInZip() on success. |
| + // On failure, current_file_info() becomes NULL. |
| + bool LocateAndOpenFile(const FilePath& path_in_zip); |
| + |
| + // Extracts the current file to the given output file path. If the |
| + // current file is a directory, just creates a directory |
| + // instead. Returns true on success. OpenCurrentFileInZip() must be |
| + // called beforehand. |
| + // |
| + // This function does not preserve the timestamp of the original file. |
| + bool ExtractCurrentFileToFile(const FilePath& output_file_path); |
|
Aaron Boodman
2011/11/09 08:00:40
Throughout the API, consider using the term 'entry
satorux1
2011/11/10 08:00:56
Good idea. Changed accordingly.
|
| + |
| + // Extracts the current file to the given output directory path using |
| + // ExtractCurrentFileToFile(). Sub directories are created as needed |
| + // based on the file path of the current file. For example, if the file |
| + // path in zip is "foo/bar.txt", and the output directory is "output", |
| + // "output/foo/bar.txt" will be created. |
| + // |
| + // Returns true on success. OpenCurrentFileInZip() must be called |
| + // beforehand. |
| + bool ExtractCurrentFileToDirectory(const FilePath& output_directory_path); |
|
Aaron Boodman
2011/11/09 08:00:40
Maybe "ExtractCurrentEntryIntoDirectory" ("into" i
satorux1
2011/11/10 08:00:56
Done.
|
| + |
| + // Returns the current file info. Returns NULL if the current file is |
| + // not yet opened. OpenCurrentFileInZip() must be called beforehand. |
| + FileInfo* current_file_info() const { |
| + return current_file_info_.get(); |
| + } |
| + |
| + // Returns the number of files in the zip file. |
| + // Open() must be called beforehand. |
| + int num_files() const { return num_files_; } |
| + |
| + private: |
| + unzFile zip_file_; |
| + int num_files_; |
| + bool reached_end_; |
| + scoped_ptr<FileInfo> current_file_info_; |
|
Aaron Boodman
2011/11/09 08:00:40
Call the DISALLOW_COPY_AND_ASSIGN macro from basic
satorux1
2011/11/10 08:00:56
Good catch. Added.
|
| +}; |
| + |
| +} // namespace zip |
| + |
| +#endif // CHROME_COMMON_ZIP_READER_H_ |