OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_COMMON_ZIP_READER_H_ | |
6 #define CHROME_COMMON_ZIP_READER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/time.h" | |
14 #include "third_party/zlib/contrib/minizip/unzip.h" | |
15 | |
16 namespace zip { | |
17 | |
18 // This class is used for reading zip files. A typical use case of this | |
19 // class is to scan files in a zip file and extract them. The code will | |
20 // look like: | |
21 // | |
22 // ZipReader reader; | |
23 // reader.Open(zip_file_path); | |
24 // while (reader.HasMore()) { | |
25 // reader.OpenCurrentFileInZip(); | |
26 // reader.ExtractCurrentFileToDirectory(output_directory_path); | |
27 // reader.AdvanceToNextFile(); | |
28 // } | |
29 // | |
30 // For simplicty, error checking is omitted in the example code above. The | |
31 // production code should check return values from all of these functions. | |
32 // | |
33 // This calls can also be used for random access of contents in a zip file | |
34 // using LocateAndOpenFile(). | |
35 // | |
36 class ZipReader { | |
Aaron Boodman
2011/11/09 08:00:40
Nice API.
satorux1
2011/11/10 08:00:56
Thanks!
| |
37 public: | |
38 // This class represents information of a file in a zip file. | |
39 class FileInfo { | |
40 public: | |
41 FileInfo(const std::string& filename_in_zip, | |
42 const unz_file_info& raw_file_info); | |
43 | |
44 // 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
| |
45 const FilePath& file_path() const { return file_path_; } | |
46 | |
47 // Returns the size of the original file (i.e. after uncompressed). | |
48 const int64 original_size() const { return original_size_; } | |
49 | |
50 // Returns the last modified time. | |
51 const base::Time last_modified() const { return last_modified_; } | |
52 | |
53 // Returns true if the file is a directory. | |
54 bool is_directory() const { return is_directory_; } | |
55 | |
56 // Returns true if the file is unsafe, like having ".." in its file | |
57 // name or invalid UTF-8 characters. | |
58 bool is_unsafe() const { return is_unsafe_; } | |
59 | |
60 private: | |
61 const FilePath file_path_; | |
62 int64 original_size_; | |
63 base::Time last_modified_; | |
64 bool is_directory_; | |
65 bool is_unsafe_; | |
66 DISALLOW_COPY_AND_ASSIGN(FileInfo); | |
67 }; | |
68 | |
69 ZipReader(); | |
70 ~ZipReader(); | |
71 | |
72 // Opens the zip file specified by |zip_file_path|. Returns true on | |
73 // success. | |
74 bool Open(const FilePath& zip_file_path); | |
75 | |
76 // Closes the currently opened zip file. This function is called in the | |
77 // destructor of the class, so you usually don't need to call this. | |
78 void Close(); | |
79 | |
80 // Returns true if there is at least one file to read. This function is | |
81 // used to scan files with AdvanceToNextFile(), like: | |
82 // | |
83 // while (reader.HasMore()) { | |
84 // // Do something with the current file here. | |
85 // reader.AdvanceToNextFile(); | |
86 // } | |
87 bool HasMore(); | |
88 | |
89 // Advances the next file. Returns true on success. | |
90 bool AdvanceToNextFile(); | |
91 | |
92 // Opens the current file in the zip file. On success, returns true and | |
93 // updates the the current file state (i.e. current_file_info() is | |
94 // updated). This function should be called before operations over the | |
95 // current file like ExtractCurrentFileToFile(). | |
96 // | |
97 // Note that there is no CloseCurrentFileInZip(). The the current file | |
98 // state is reset automatically as needed. | |
99 bool OpenCurrentFileInZip(); | |
100 | |
101 // Locates a file in the zip file and opens it. Returns true on success. | |
102 // This function internally calls OpenCurrentFileInZip() on success. | |
103 // On failure, current_file_info() becomes NULL. | |
104 bool LocateAndOpenFile(const FilePath& path_in_zip); | |
105 | |
106 // Extracts the current file to the given output file path. If the | |
107 // current file is a directory, just creates a directory | |
108 // instead. Returns true on success. OpenCurrentFileInZip() must be | |
109 // called beforehand. | |
110 // | |
111 // This function does not preserve the timestamp of the original file. | |
112 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.
| |
113 | |
114 // Extracts the current file to the given output directory path using | |
115 // ExtractCurrentFileToFile(). Sub directories are created as needed | |
116 // based on the file path of the current file. For example, if the file | |
117 // path in zip is "foo/bar.txt", and the output directory is "output", | |
118 // "output/foo/bar.txt" will be created. | |
119 // | |
120 // Returns true on success. OpenCurrentFileInZip() must be called | |
121 // beforehand. | |
122 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.
| |
123 | |
124 // Returns the current file info. Returns NULL if the current file is | |
125 // not yet opened. OpenCurrentFileInZip() must be called beforehand. | |
126 FileInfo* current_file_info() const { | |
127 return current_file_info_.get(); | |
128 } | |
129 | |
130 // Returns the number of files in the zip file. | |
131 // Open() must be called beforehand. | |
132 int num_files() const { return num_files_; } | |
133 | |
134 private: | |
135 unzFile zip_file_; | |
136 int num_files_; | |
137 bool reached_end_; | |
138 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.
| |
139 }; | |
140 | |
141 } // namespace zip | |
142 | |
143 #endif // CHROME_COMMON_ZIP_READER_H_ | |
OLD | NEW |