blob: 85ca1aed4edccf6acd786de7dfe5d733d4d6b59d [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 Lesinskia40e9722015-11-24 19:11:46 -080024#include "io/File.h"
25#include "util/StringPiece.h"
26
Adam Lesinskia40e9722015-11-24 19:11:46 -080027namespace aapt {
28namespace io {
29
30/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 * An IFile representing a file within a ZIP archive. If the file is compressed,
32 * it is uncompressed
33 * and copied into memory when opened. Otherwise it is mmapped from the ZIP
34 * archive.
Adam Lesinskia40e9722015-11-24 19:11:46 -080035 */
36class ZipFile : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 public:
38 ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source);
Adam Lesinskia40e9722015-11-24 19:11:46 -080039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 std::unique_ptr<IData> OpenAsData() override;
41 const Source& GetSource() const override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 ZipArchiveHandle zip_handle_;
45 ZipEntry zip_entry_;
46 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:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 std::map<std::string, std::unique_ptr<IFile>>::const_iterator current_, end_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080060};
61
Adam Lesinskia40e9722015-11-24 19:11:46 -080062/**
63 * An IFileCollection that represents a ZIP archive and the entries within it.
64 */
65class ZipFileCollection : public IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 static std::unique_ptr<ZipFileCollection> Create(const StringPiece& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 std::string* outError);
Adam Lesinskia40e9722015-11-24 19:11:46 -080069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 io::IFile* FindFile(const StringPiece& path) override;
71 std::unique_ptr<IFileCollectionIterator> Iterator() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ~ZipFileCollection() override;
Adam Lesinskia40e9722015-11-24 19:11:46 -080074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 private:
76 friend class ZipFileCollectionIterator;
77 ZipFileCollection();
Adam Lesinskia40e9722015-11-24 19:11:46 -080078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 ZipArchiveHandle handle_;
80 std::map<std::string, std::unique_ptr<IFile>> files_;
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 */