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