• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef AAPT_IO_ZIPARCHIVE_H
18 #define AAPT_IO_ZIPARCHIVE_H
19 
20 #include "io/File.h"
21 #include "util/StringPiece.h"
22 
23 #include <map>
24 #include <ziparchive/zip_archive.h>
25 
26 namespace aapt {
27 namespace io {
28 
29 /**
30  * An IFile representing a file within a ZIP archive. If the file is compressed, it is uncompressed
31  * and copied into memory when opened. Otherwise it is mmapped from the ZIP archive.
32  */
33 class ZipFile : public IFile {
34 public:
35     ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source);
36 
37     std::unique_ptr<IData> openAsData() override;
38     const Source& getSource() const override;
39 
40 private:
41     ZipArchiveHandle mZipHandle;
42     ZipEntry mZipEntry;
43     Source mSource;
44 };
45 
46 class ZipFileCollection;
47 
48 class ZipFileCollectionIterator : public IFileCollectionIterator {
49 public:
50     ZipFileCollectionIterator(ZipFileCollection* collection);
51 
52     bool hasNext() override;
53     io::IFile* next() override;
54 
55 private:
56     std::map<std::string, std::unique_ptr<IFile>>::const_iterator mCurrent, mEnd;
57 };
58 
59 /**
60  * An IFileCollection that represents a ZIP archive and the entries within it.
61  */
62 class ZipFileCollection : public IFileCollection {
63 public:
64     static std::unique_ptr<ZipFileCollection> create(const StringPiece& path,
65                                                      std::string* outError);
66 
67     io::IFile* findFile(const StringPiece& path) override;
68     std::unique_ptr<IFileCollectionIterator> iterator() override;
69 
70     ~ZipFileCollection() override;
71 
72 private:
73     friend class ZipFileCollectionIterator;
74     ZipFileCollection();
75 
76     ZipArchiveHandle mHandle;
77     std::map<std::string, std::unique_ptr<IFile>> mFiles;
78 };
79 
80 } // namespace io
81 } // namespace aapt
82 
83 #endif /* AAPT_IO_ZIPARCHIVE_H */
84