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