• 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 "ziparchive/zip_archive.h"
21 
22 #include <map>
23 
24 #include "androidfw/StringPiece.h"
25 
26 #include "io/File.h"
27 
28 namespace aapt {
29 namespace io {
30 
31 /**
32  * An IFile representing a file within a ZIP archive. If the file is compressed,
33  * it is uncompressed
34  * and copied into memory when opened. Otherwise it is mmapped from the ZIP
35  * archive.
36  */
37 class ZipFile : public IFile {
38  public:
39   ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source);
40 
41   std::unique_ptr<IData> OpenAsData() override;
42   const Source& GetSource() const override;
43   bool WasCompressed() override;
44 
45  private:
46   ZipArchiveHandle zip_handle_;
47   ZipEntry zip_entry_;
48   Source source_;
49 };
50 
51 class ZipFileCollection;
52 
53 class ZipFileCollectionIterator : public IFileCollectionIterator {
54  public:
55   explicit ZipFileCollectionIterator(ZipFileCollection* collection);
56 
57   bool HasNext() override;
58   io::IFile* Next() override;
59 
60  private:
61   std::vector<std::unique_ptr<IFile>>::const_iterator current_, end_;
62 };
63 
64 /**
65  * An IFileCollection that represents a ZIP archive and the entries within it.
66  */
67 class ZipFileCollection : public IFileCollection {
68  public:
69   static std::unique_ptr<ZipFileCollection> Create(const android::StringPiece& path,
70                                                    std::string* outError);
71 
72   io::IFile* FindFile(const android::StringPiece& path) override;
73   std::unique_ptr<IFileCollectionIterator> Iterator() override;
74 
75   ~ZipFileCollection() override;
76 
77  private:
78   friend class ZipFileCollectionIterator;
79   ZipFileCollection();
80 
81   ZipArchiveHandle handle_;
82   std::vector<std::unique_ptr<IFile>> files_;
83   std::map<std::string, IFile*> files_by_name_;
84 };
85 
86 }  // namespace io
87 }  // namespace aapt
88 
89 #endif /* AAPT_IO_ZIPARCHIVE_H */
90