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

Side by Side Diff: chrome/common/zip_reader.h

Issue 8508003: zip: Add ZipReader and rework Unzip() using the new class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 9 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/zip.cc ('k') | chrome/common/zip_reader.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/basictypes.h"
12 #include "base/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time.h"
15 #include "third_party/zlib/contrib/minizip/unzip.h"
16
17 namespace zip {
18
19 // This class is used for reading zip files. A typical use case of this
20 // class is to scan entries in a zip file and extract them. The code will
21 // look like:
22 //
23 // ZipReader reader;
24 // reader.Open(zip_file_path);
25 // while (reader.HasMore()) {
26 // reader.OpenCurrentEntryInZip();
27 // reader.ExtractCurrentEntryToDirectory(output_directory_path);
28 // reader.AdvanceToNextEntry();
29 // }
30 //
31 // For simplicty, error checking is omitted in the example code above. The
32 // production code should check return values from all of these functions.
33 //
34 // This calls can also be used for random access of contents in a zip file
35 // using LocateAndOpenEntry().
36 //
37 class ZipReader {
38 public:
39 // This class represents information of an entry (file or directory) in
40 // a zip file.
41 class EntryInfo {
42 public:
43 EntryInfo(const std::string& filename_in_zip,
44 const unz_file_info& raw_file_info);
45
46 // Returns the file path. The path is usually relative like
47 // "foo/bar.txt", but if it's absolute, is_unsafe() returns true.
Aaron Boodman 2011/11/10 08:13:05 Is it legit for zip files to have paths starting w
satorux1 2011/11/10 17:42:26 Good question. Per the spec, it seems to legitimat
48 const FilePath& file_path() const { return file_path_; }
49
50 // Returns the size of the original file (i.e. after uncompressed).
51 // Returns 0 if the entry is a directory.
52 const int64 original_size() const { return original_size_; }
53
54 // Returns the last modified time.
55 const base::Time last_modified() const { return last_modified_; }
56
57 // Returns true if the entry is a directory.
58 bool is_directory() const { return is_directory_; }
59
60 // Returns true if the entry is unsafe, like having ".." or invalid
61 // UTF-8 characters in its file name, or the file path is absolute.
62 bool is_unsafe() const { return is_unsafe_; }
63
64 private:
65 const FilePath file_path_;
66 int64 original_size_;
67 base::Time last_modified_;
68 bool is_directory_;
69 bool is_unsafe_;
70 DISALLOW_COPY_AND_ASSIGN(EntryInfo);
71 };
72
73 ZipReader();
74 ~ZipReader();
75
76 // Opens the zip file specified by |zip_file_path|. Returns true on
77 // success.
78 bool Open(const FilePath& zip_file_path);
79
80 // Closes the currently opened zip file. This function is called in the
81 // destructor of the class, so you usually don't need to call this.
82 void Close();
83
84 // Returns true if there is at least one entry to read. This function is
85 // used to scan entries with AdvanceToNextEntry(), like:
86 //
87 // while (reader.HasMore()) {
88 // // Do something with the current file here.
89 // reader.AdvanceToNextEntry();
90 // }
91 bool HasMore();
92
93 // Advances the next entry. Returns true on success.
94 bool AdvanceToNextEntry();
95
96 // Opens the current entry in the zip file. On success, returns true and
97 // updates the the current entry state (i.e. current_entry_info() is
98 // updated). This function should be called before operations over the
99 // current entry like ExtractCurrentEntryToFile().
100 //
101 // Note that there is no CloseCurrentEntryInZip(). The the current entry
102 // state is reset automatically as needed.
103 bool OpenCurrentEntryInZip();
104
105 // Locates an entry in the zip file and opens it. Returns true on
106 // success. This function internally calls OpenCurrentEntryInZip() on
107 // success. On failure, current_entry_info() becomes NULL.
108 bool LocateAndOpenEntry(const FilePath& path_in_zip);
109
110 // Extracts the current entry to the given output file path. If the
111 // current file is a directory, just creates a directory
112 // instead. Returns true on success. OpenCurrentEntryInZip() must be
113 // called beforehand.
114 //
115 // This function does not preserve the timestamp of the original entry.
116 bool ExtractCurrentEntryToFilePath(const FilePath& output_file_path);
117
118 // Extracts the current entry to the given output directory path using
119 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed
120 // based on the file path of the current entry. For example, if the file
121 // path in zip is "foo/bar.txt", and the output directory is "output",
122 // "output/foo/bar.txt" will be created.
123 //
124 // Returns true on success. OpenCurrentEntryInZip() must be called
125 // beforehand.
126 bool ExtractCurrentEntryIntoDirectory(const FilePath& output_directory_path);
127
128 // Returns the current entry info. Returns NULL if the current entry is
129 // not yet opened. OpenCurrentEntryInZip() must be called beforehand.
130 EntryInfo* current_entry_info() const {
131 return current_entry_info_.get();
132 }
133
134 // Returns the number of entries in the zip file.
135 // Open() must be called beforehand.
136 int num_entries() const { return num_entries_; }
137
138 private:
139 // Resets the internal state.
140 void Reset();
141
142 unzFile zip_file_;
143 int num_entries_;
144 bool reached_end_;
145 scoped_ptr<EntryInfo> current_entry_info_;
146
147 DISALLOW_COPY_AND_ASSIGN(ZipReader);
148 };
149
150 } // namespace zip
151
152 #endif // CHROME_COMMON_ZIP_READER_H_
OLDNEW
« no previous file with comments | « chrome/common/zip.cc ('k') | chrome/common/zip_reader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698