blob: 8381259d77c5c092b4a7a30b9a6a3e125a58103b [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include "ziparchive/zip_archive.h"
21
22#include <map>
23
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25
Adam Lesinskia40e9722015-11-24 19:11:46 -080026#include "io/File.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080027
Adam Lesinskia40e9722015-11-24 19:11:46 -080028namespace aapt {
29namespace io {
30
Adam Lesinski00451162017-10-03 07:44:08 -070031// An IFile representing a file within a ZIP archive. If the file is compressed, it is uncompressed
32// and copied into memory when opened. Otherwise it is mmapped from the ZIP archive.
Adam Lesinskia40e9722015-11-24 19:11:46 -080033class ZipFile : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 public:
Adam Lesinski00451162017-10-03 07:44:08 -070035 ZipFile(::ZipArchiveHandle handle, const ::ZipEntry& entry, const Source& source);
Adam Lesinskia40e9722015-11-24 19:11:46 -080036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 std::unique_ptr<IData> OpenAsData() override;
Adam Lesinski00451162017-10-03 07:44:08 -070038 std::unique_ptr<io::InputStream> OpenInputStream() override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 const Source& GetSource() const override;
Pierre Lecesne970732d2017-02-02 23:13:58 +000040 bool WasCompressed() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 private:
Adam Lesinski00451162017-10-03 07:44:08 -070043 ::ZipArchiveHandle zip_handle_;
44 ::ZipEntry zip_entry_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 Source source_;
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 Lesinskice5e56e2016-10-21 17:56:45 -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:
Pierre Lecesne880d65b2017-02-02 22:33:17 +000058 std::vector<std::unique_ptr<IFile>>::const_iterator current_, end_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080059};
60
Adam Lesinski00451162017-10-03 07:44:08 -070061// An IFileCollection that represents a ZIP archive and the entries within it.
Adam Lesinskia40e9722015-11-24 19:11:46 -080062class ZipFileCollection : public IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080064 static std::unique_ptr<ZipFileCollection> Create(const android::StringPiece& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 std::string* outError);
Adam Lesinskia40e9722015-11-24 19:11:46 -080066
Adam Lesinskid5083f62017-01-16 15:07:21 -080067 io::IFile* FindFile(const android::StringPiece& path) override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 std::unique_ptr<IFileCollectionIterator> Iterator() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ~ZipFileCollection() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 private:
73 friend class ZipFileCollectionIterator;
74 ZipFileCollection();
Adam Lesinskia40e9722015-11-24 19:11:46 -080075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 ZipArchiveHandle handle_;
Pierre Lecesne880d65b2017-02-02 22:33:17 +000077 std::vector<std::unique_ptr<IFile>> files_;
78 std::map<std::string, IFile*> files_by_name_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080079};
80
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081} // namespace io
82} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -080083
84#endif /* AAPT_IO_ZIPARCHIVE_H */