zip_archive.h revision 507dfdd147c97bfbadebfd63584d094b6a4e7b47
1/*
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
17#ifndef ART_RUNTIME_ZIP_ARCHIVE_H_
18#define ART_RUNTIME_ZIP_ARCHIVE_H_
19
20#include <stdint.h>
21#include <string>
22#include <ziparchive/zip_archive.h>
23
24#include "base/logging.h"
25#include "base/unix_file/random_access_file.h"
26#include "globals.h"
27#include "mem_map.h"
28#include "os.h"
29#include "safe_map.h"
30#include "UniquePtrCompat.h"
31
32namespace art {
33
34class ZipArchive;
35class MemMap;
36
37class ZipEntry {
38 public:
39  bool ExtractToFile(File& file, std::string* error_msg);
40  MemMap* ExtractToMemMap(const char* entry_filename, std::string* error_msg);
41  virtual ~ZipEntry();
42
43  uint32_t GetUncompressedLength();
44  uint32_t GetCrc32();
45
46 private:
47  ZipEntry(ZipArchiveHandle handle,
48           ::ZipEntry* zip_entry) : handle_(handle), zip_entry_(zip_entry) {}
49
50  ZipArchiveHandle handle_;
51  ::ZipEntry* const zip_entry_;
52
53  friend class ZipArchive;
54  DISALLOW_COPY_AND_ASSIGN(ZipEntry);
55};
56
57class ZipArchive {
58 public:
59  // return new ZipArchive instance on success, NULL on error.
60  static ZipArchive* Open(const char* filename, std::string* error_msg);
61  static ZipArchive* OpenFromFd(int fd, const char* filename, std::string* error_msg);
62
63  ZipEntry* Find(const char* name, std::string* error_msg) const;
64
65  ~ZipArchive() {
66    CloseArchive(handle_);
67  }
68
69 private:
70  explicit ZipArchive(ZipArchiveHandle handle) : handle_(handle) {}
71
72  friend class ZipEntry;
73
74  ZipArchiveHandle handle_;
75
76  DISALLOW_COPY_AND_ASSIGN(ZipArchive);
77};
78
79}  // namespace art
80
81#endif  // ART_RUNTIME_ZIP_ARCHIVE_H_
82