blob: e04525f51f4b9f954ed672f5909c8ee03d4e79f7 [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 Lesinskia40e9722015-11-24 19:11:46 -080023#include <ziparchive/zip_archive.h>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024#include <map>
Adam Lesinskia40e9722015-11-24 19:11:46 -080025
26namespace aapt {
27namespace io {
28
29/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 * 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 Lesinskia40e9722015-11-24 19:11:46 -080034 */
35class ZipFile : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 public:
37 ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source);
Adam Lesinskia40e9722015-11-24 19:11:46 -080038
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 std::unique_ptr<IData> openAsData() override;
40 const Source& getSource() const override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 private:
43 ZipArchiveHandle mZipHandle;
44 ZipEntry mZipEntry;
45 Source mSource;
Adam Lesinskia40e9722015-11-24 19:11:46 -080046};
47
Adam Lesinskia6fe3452015-12-09 15:20:52 -080048class ZipFileCollection;
49
50class ZipFileCollectionIterator : public IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 public:
52 explicit ZipFileCollectionIterator(ZipFileCollection* collection);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 bool hasNext() override;
55 io::IFile* next() override;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 private:
58 std::map<std::string, std::unique_ptr<IFile>>::const_iterator mCurrent, mEnd;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080059};
60
Adam Lesinskia40e9722015-11-24 19:11:46 -080061/**
62 * An IFileCollection that represents a ZIP archive and the entries within it.
63 */
64class ZipFileCollection : public IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 public:
66 static std::unique_ptr<ZipFileCollection> create(const StringPiece& path,
67 std::string* outError);
Adam Lesinskia40e9722015-11-24 19:11:46 -080068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 io::IFile* findFile(const StringPiece& path) override;
70 std::unique_ptr<IFileCollectionIterator> iterator() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 ~ZipFileCollection() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 private:
75 friend class ZipFileCollectionIterator;
76 ZipFileCollection();
Adam Lesinskia40e9722015-11-24 19:11:46 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 ZipArchiveHandle mHandle;
79 std::map<std::string, std::unique_ptr<IFile>> mFiles;
Adam Lesinskia40e9722015-11-24 19:11:46 -080080};
81
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082} // namespace io
83} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -080084
85#endif /* AAPT_IO_ZIPARCHIVE_H */