blob: a53c4a2e39f885a4736142d2dc52ab8b5abc0d20 [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:
Jeremy Meyer56f36e82022-05-20 20:35:42 +000035 ZipFile(::ZipArchiveHandle handle, const ::ZipEntry& entry, const android::Source& source);
Adam Lesinskia40e9722015-11-24 19:11:46 -080036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 std::unique_ptr<IData> OpenAsData() override;
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000038 std::unique_ptr<android::InputStream> OpenInputStream() override;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000039 const android::Source& GetSource() const override;
Pierre Lecesne970732d2017-02-02 23:13:58 +000040 bool WasCompressed() override;
Mark Punzalane5671592023-09-02 00:00:30 +000041 bool GetModificationTime(struct tm* buf) const override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 private:
Adam Lesinski00451162017-10-03 07:44:08 -070044 ::ZipArchiveHandle zip_handle_;
45 ::ZipEntry zip_entry_;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000046 android::Source source_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080047};
48
Adam Lesinskia6fe3452015-12-09 15:20:52 -080049class ZipFileCollection;
50
51class ZipFileCollectionIterator : public IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 public:
53 explicit ZipFileCollectionIterator(ZipFileCollection* collection);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 bool HasNext() override;
56 io::IFile* Next() override;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 private:
Pierre Lecesne880d65b2017-02-02 22:33:17 +000059 std::vector<std::unique_ptr<IFile>>::const_iterator current_, end_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080060};
61
Adam Lesinski00451162017-10-03 07:44:08 -070062// An IFileCollection that represents a ZIP archive and the entries within it.
Adam Lesinskia40e9722015-11-24 19:11:46 -080063class ZipFileCollection : public IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 public:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070065 static std::unique_ptr<ZipFileCollection> Create(android::StringPiece path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 std::string* outError);
Adam Lesinskia40e9722015-11-24 19:11:46 -080067
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070068 io::IFile* FindFile(android::StringPiece path) override;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 std::unique_ptr<IFileCollectionIterator> Iterator() override;
Ryan Mitchell0ce89732018-10-03 09:20:57 -070070 char GetDirSeparator() 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 Lesinskice5e56e2016-10-21 17:56:45 -070078 ZipArchiveHandle handle_;
Pierre Lecesne880d65b2017-02-02 22:33:17 +000079 std::vector<std::unique_ptr<IFile>> files_;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070080 std::map<std::string, IFile*, std::less<>> files_by_name_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080081};
82
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083} // namespace io
84} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -080085
86#endif /* AAPT_IO_ZIPARCHIVE_H */