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 | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 23 | #include <map> |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 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: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 35 | ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 36 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 37 | std::unique_ptr<IData> openAsData() override; |
| 38 | const Source& getSource() const override; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | ZipArchiveHandle mZipHandle; |
| 42 | ZipEntry mZipEntry; |
| 43 | Source mSource; |
| 44 | }; |
| 45 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 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, |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 65 | std::string* outError); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 66 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 67 | io::IFile* findFile(const StringPiece& path) override; |
| 68 | std::unique_ptr<IFileCollectionIterator> iterator() override; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 69 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 70 | ~ZipFileCollection() override; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 71 | |
| 72 | private: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 73 | friend class ZipFileCollectionIterator; |
| 74 | ZipFileCollection(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 75 | |
| 76 | ZipArchiveHandle mHandle; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 77 | std::map<std::string, std::unique_ptr<IFile>> mFiles; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | } // namespace io |
| 81 | } // namespace aapt |
| 82 | |
| 83 | #endif /* AAPT_IO_ZIPARCHIVE_H */ |