blob: 717eb8c82edd56b0d31de07127fe0e2e26ccabdf [file] [log] [blame]
Brian Carlstromb0460ea2011-07-29 10:08:05 -07001/*
2 * Copyright (C) 2008 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_ZIP_ARCHIVE_H_
18#define ART_RUNTIME_ZIP_ARCHIVE_H_
Brian Carlstromb0460ea2011-07-29 10:08:05 -070019
Brian Carlstromb0460ea2011-07-29 10:08:05 -070020#include <stdint.h>
Narayan Kamath92572be2013-11-28 14:06:24 +000021#include <ziparchive/zip_archive.h>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
23#include <string>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/random_access_file.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070027#include "globals.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028#include "mem_map.h"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070030#include "safe_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070031
32namespace art {
33
34class ZipArchive;
35class MemMap;
36
37class ZipEntry {
Brian Carlstromb0460ea2011-07-29 10:08:05 -070038 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070039 bool ExtractToFile(File& file, std::string* error_msg);
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070040 MemMap* ExtractToMemMap(const char* zip_filename, const char* entry_filename,
41 std::string* error_msg);
Mathieu Chartier661974a2014-01-09 11:23:53 -080042 virtual ~ZipEntry();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070043
Brian Carlstrom89521892011-12-07 22:05:07 -080044 uint32_t GetUncompressedLength();
Brian Carlstromb0460ea2011-07-29 10:08:05 -070045 uint32_t GetCrc32();
46
47 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000048 ZipEntry(ZipArchiveHandle handle,
49 ::ZipEntry* zip_entry) : handle_(handle), zip_entry_(zip_entry) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070050
Narayan Kamath92572be2013-11-28 14:06:24 +000051 ZipArchiveHandle handle_;
52 ::ZipEntry* const zip_entry_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070053
54 friend class ZipArchive;
Elliott Hughesa21039c2012-06-21 12:09:25 -070055 DISALLOW_COPY_AND_ASSIGN(ZipEntry);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070056};
57
Brian Carlstromb0460ea2011-07-29 10:08:05 -070058class ZipArchive {
59 public:
Mathieu Chartier2cebb242015-04-21 16:50:40 -070060 // return new ZipArchive instance on success, null on error.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070061 static ZipArchive* Open(const char* filename, std::string* error_msg);
62 static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070063
Narayan Kamath92572be2013-11-28 14:06:24 +000064 ZipEntry* Find(const char* name, std::string* error_msg) const;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070065
66 ~ZipArchive() {
Narayan Kamath92572be2013-11-28 14:06:24 +000067 CloseArchive(handle_);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070068 }
69
70 private:
Narayan Kamath92572be2013-11-28 14:06:24 +000071 explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070072
73 friend class ZipEntry;
Elliott Hughesa21039c2012-06-21 12:09:25 -070074
Narayan Kamath92572be2013-11-28 14:06:24 +000075 ZipArchiveHandle handle_;
76
Elliott Hughesa21039c2012-06-21 12:09:25 -070077 DISALLOW_COPY_AND_ASSIGN(ZipArchive);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070078};
79
80} // namespace art
81
Brian Carlstromfc0e3212013-07-17 14:40:12 -070082#endif // ART_RUNTIME_ZIP_ARCHIVE_H_