blob: 5ad119d1d6d4795a7a420df359c79c87b9ef6d2f [file] [log] [blame]
Adam Lesinskia40e9722015-11-24 19:11:46 -08001/*
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 Lesinskia6fe3452015-12-09 15:20:52 -080023#include <map>
Adam Lesinskia40e9722015-11-24 19:11:46 -080024#include <ziparchive/zip_archive.h>
25
26namespace aapt {
27namespace 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 */
33class ZipFile : public IFile {
34public:
Adam Lesinskia6fe3452015-12-09 15:20:52 -080035 ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source);
Adam Lesinskia40e9722015-11-24 19:11:46 -080036
Adam Lesinskia6fe3452015-12-09 15:20:52 -080037 std::unique_ptr<IData> openAsData() override;
38 const Source& getSource() const override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080039
40private:
41 ZipArchiveHandle mZipHandle;
42 ZipEntry mZipEntry;
43 Source mSource;
44};
45
Adam Lesinskia6fe3452015-12-09 15:20:52 -080046class ZipFileCollection;
47
48class ZipFileCollectionIterator : public IFileCollectionIterator {
49public:
50 ZipFileCollectionIterator(ZipFileCollection* collection);
51
52 bool hasNext() override;
53 io::IFile* next() override;
54
55private:
56 std::map<std::string, std::unique_ptr<IFile>>::const_iterator mCurrent, mEnd;
57};
58
Adam Lesinskia40e9722015-11-24 19:11:46 -080059/**
60 * An IFileCollection that represents a ZIP archive and the entries within it.
61 */
62class ZipFileCollection : public IFileCollection {
63public:
64 static std::unique_ptr<ZipFileCollection> create(const StringPiece& path,
Adam Lesinskia6fe3452015-12-09 15:20:52 -080065 std::string* outError);
Adam Lesinskia40e9722015-11-24 19:11:46 -080066
Adam Lesinskia6fe3452015-12-09 15:20:52 -080067 io::IFile* findFile(const StringPiece& path) override;
68 std::unique_ptr<IFileCollectionIterator> iterator() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080069
Adam Lesinskia6fe3452015-12-09 15:20:52 -080070 ~ZipFileCollection() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080071
72private:
Adam Lesinskia6fe3452015-12-09 15:20:52 -080073 friend class ZipFileCollectionIterator;
74 ZipFileCollection();
Adam Lesinskia40e9722015-11-24 19:11:46 -080075
76 ZipArchiveHandle mHandle;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080077 std::map<std::string, std::unique_ptr<IFile>> mFiles;
Adam Lesinskia40e9722015-11-24 19:11:46 -080078};
79
80} // namespace io
81} // namespace aapt
82
83#endif /* AAPT_IO_ZIPARCHIVE_H */