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