Add support for opening classes.dex file from zip, jar, apk

Adding new ZipArchive class and test

	src/zip_archive.h
	src/zip_archive.cc
	src/zip_archive_test.cc
	build/Android.common.mk

Change from host only use of build dex file for libcore to using host
and target core.jar containing classes.dex files. This requires
setting up an ANDROID_DATA directory to containing an art-cache file
for the extracted dex files, similar to the dalvik-cache for odex
files. A unique ANDROID_DATA and art-cache is created and cleaned up
for each test run (similar to vogar).

	src/common_test.h

Add dependency for libcore jar files to test targets to support
RuntimeTest use of core.jar

	Android.mk

Extract common includes to ART_C_INCLUDES when adding zlib dependency

	build/Android.common.mk
	build/Android.libart.mk
	build/Android.test.mk

Adding TODO regarding unordered map for ClassLinker::classes_ table.

	src/class_linker.h

Adding DexFile::OpenZip (also changed OpenFile to take
	src/dex_file.cc
	src/dex_file.h

Adding kPageSize of 4096, validated by Runtime::Init

	src/globals.h
	src/runtime.cc

    Updated to use kPageSize where it seemed appropriate.

	src/jni_compiler.cc
	src/jni_compiler_test.cc
	src/space.cc
	src/thread.cc
	src/thread_x86.cc

Changed thread_list_ and class_linker_ to be declared in Runtime::Init
initialization order.

	src/runtime.h

Change-Id: Id626abe5b6c1990e4f93598256ee0fae000818f6
diff --git a/Android.mk b/Android.mk
index 0d1fa04..f1e2a73 100644
--- a/Android.mk
+++ b/Android.mk
@@ -41,12 +41,12 @@
 
 # "mm test-art-host" to build and run all host tests
 .PHONY: test-art-host
-test-art-host: $(ART_HOST_TEST_EXECUTABLES)
+test-art-host: $(ART_HOST_TEST_EXECUTABLES) $(ANDROID_HOST_OUT)/framework/core-hostdex.jar
 	$(foreach file,$(ART_HOST_TEST_EXECUTABLES),$(file) &&) true
 
 # "mm test-art-device" to build and run all target tests
 .PHONY: test-art-target
-test-art-target: $(ART_TARGET_TEST_EXECUTABLES)
+test-art-target: $(ART_TARGET_TEST_EXECUTABLES) $(ANDROID_PRODUCT_OUT)/system/framework/core.jar
 	adb remount
 	adb sync
 	adb shell touch /sdcard/test-art-target
diff --git a/build/Android.common.mk b/build/Android.common.mk
index f92bc09..64f0de4 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -16,6 +16,8 @@
 
 ART_CPP_EXTENSION := .cc
 
+ART_C_INCLUDES := external/gtest/include external/zlib
+
 ART_CFLAGS := \
 	-O0 \
 	-ggdb3 \
@@ -52,7 +54,8 @@
 	src/space.cc \
 	src/stringpiece.cc \
 	src/stringprintf.cc \
-	src/thread.cc
+	src/thread.cc \
+	src/zip_archive.cc
 
 LIBART_TARGET_SRC_FILES := \
 	$(LIBART_COMMON_SRC_FILES) \
@@ -83,7 +86,8 @@
 	src/jni_compiler_test.cc.arm \
 	src/object_test.cc \
 	src/runtime_test.cc \
-	src/space_test.cc
+	src/space_test.cc \
+	src/zip_archive_test.cc
 
 TEST_TARGET_SRC_FILES := \
 	$(TEST_COMMON_SRC_FILES) \
diff --git a/build/Android.libart.mk b/build/Android.libart.mk
index de07b8c..e6169e9 100644
--- a/build/Android.libart.mk
+++ b/build/Android.libart.mk
@@ -37,7 +37,7 @@
   ifeq ($(2),debug)
     LOCAL_CFLAGS += -UNDEBUG
   endif
-  LOCAL_C_INCLUDES += src external/gtest/include
+  LOCAL_C_INCLUDES += src $(ART_C_INCLUDES)
   LOCAL_SHARED_LIBRARIES := liblog
   ifeq ($(1),target)
     LOCAL_SHARED_LIBRARIES += libcutils libstlport libz
diff --git a/build/Android.test.mk b/build/Android.test.mk
index 5d2a225..325f076 100644
--- a/build/Android.test.mk
+++ b/build/Android.test.mk
@@ -29,12 +29,13 @@
   LOCAL_MODULE_TAGS := tests
   LOCAL_SRC_FILES := $(2)
   LOCAL_CFLAGS := $(ART_CFLAGS)
-  LOCAL_C_INCLUDES += external/gtest/include
+  LOCAL_C_INCLUDES += $(ART_C_INCLUDES)
   LOCAL_SHARED_LIBRARIES := libarttest libartd
   ifeq ($(1),target)
-    LOCAL_SHARED_LIBRARIES += libstlport
+    LOCAL_SHARED_LIBRARIES += libstlport libz
     LOCAL_STATIC_LIBRARIES := libgtest libgtest_main
   else
+    LOCAL_SHARED_LIBRARIES += libz-host
     LOCAL_WHOLE_STATIC_LIBRARIES := libgtest_host libgtest_main_host
   endif
   ifeq ($(1),target)
diff --git a/src/class_linker.h b/src/class_linker.h
index a4a9b2e..87098c6 100644
--- a/src/class_linker.h
+++ b/src/class_linker.h
@@ -164,7 +164,7 @@
 
   std::vector<DexCache*> dex_caches_;
 
-  // TODO: multimap
+  // TODO: unordered_multimap
   typedef std::map<const StringPiece, Class*> Table;
 
   Table classes_;
diff --git a/src/common_test.h b/src/common_test.h
index cfc0a5f..f5d7059 100644
--- a/src/common_test.h
+++ b/src/common_test.h
@@ -1,5 +1,9 @@
 // Copyright 2011 Google Inc. All Rights Reserved.
 
+#include <dirent.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
 #include "base64.h"
 #include "heap.h"
 #include "thread.h"
@@ -189,6 +193,17 @@
   virtual void SetUp() {
     is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
 
+    android_data_.reset(strdup(is_host_ ? "/tmp/art-data-XXXXXX" : "/sdcard/art-data-XXXXXX"));
+    ASSERT_TRUE(android_data_ != NULL);
+    const char* android_data_modified = mkdtemp(android_data_.get());
+    // note that mkdtemp side effects android_data_ as well
+    ASSERT_TRUE(android_data_modified != NULL);
+    setenv("ANDROID_DATA", android_data_modified, 1);
+    art_cache_.append(android_data_.get());
+    art_cache_.append("/art-cache");
+    int mkdir_result = mkdir(art_cache_.c_str(), 0700);
+    ASSERT_EQ(mkdir_result, 0);
+
     java_lang_dex_file_.reset(OpenDexFileBase64(kJavaLangDex));
 
     std::vector<DexFile*> boot_class_path;
@@ -199,16 +214,47 @@
     class_linker_ = runtime_->GetClassLinker();
   }
 
-  DexFile* GetLibCoreDex() {
-    // TODO add host support when we have DexFile::OpenJar
-    // TODO switch to jar when we have DexFile::OpenJar
-    if (!is_host_) {
-      return NULL;
+  virtual void TearDown() {
+    const char* android_data = getenv("ANDROID_DATA");
+    ASSERT_TRUE(android_data != NULL);
+    DIR* dir = opendir(art_cache_.c_str());
+    ASSERT_TRUE(dir != NULL);
+    while (true) {
+      struct dirent entry;
+      struct dirent* entry_ptr;
+      int readdir_result = readdir_r(dir, &entry, &entry_ptr);
+      ASSERT_EQ(0, readdir_result);
+      if (entry_ptr == NULL) {
+        break;
+      }
+      if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
+        continue;
+      }
+      std::string filename(art_cache_);
+      filename.push_back('/');
+      filename.append(entry_ptr->d_name);
+      int unlink_result = unlink(filename.c_str());
+      ASSERT_EQ(0, unlink_result);
     }
+    closedir(dir);
+    int rmdir_cache_result = rmdir(art_cache_.c_str());
+    ASSERT_EQ(0, rmdir_cache_result);
+    int rmdir_data_result = rmdir(android_data_.get());
+    ASSERT_EQ(0, rmdir_data_result);
+  }
 
-    std::string libcore_dex_file_name = StringPrintf("%s/out/target/common/obj/JAVA_LIBRARIES/core_intermediates/noproguard.classes.dex",
-                                                     getenv("ANDROID_BUILD_TOP"));
-    return DexFile::OpenFile(libcore_dex_file_name.c_str());
+  std::string GetLibCoreDexFileName() {
+    if (is_host_) {
+      const char* host_dir = getenv("ANDROID_HOST_OUT");
+      CHECK(host_dir != NULL);
+      return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
+    }
+    return std::string("/system/framework/core.jar");
+  }
+
+  DexFile* GetLibCoreDex() {
+    std::string libcore_dex_file_name = GetLibCoreDexFileName();
+    return DexFile::OpenZip(libcore_dex_file_name.c_str());
   }
 
   void UseLibCoreDex() {
@@ -224,6 +270,8 @@
   }
 
   bool is_host_;
+  scoped_ptr_malloc<char> android_data_;
+  std::string art_cache_;
   scoped_ptr<DexFile> java_lang_dex_file_;
   scoped_ptr<Runtime> runtime_;
   ClassLinker* class_linker_;
diff --git a/src/dex_file.cc b/src/dex_file.cc
index a5ac01b..b61babf 100644
--- a/src/dex_file.cc
+++ b/src/dex_file.cc
@@ -3,17 +3,22 @@
 #include "dex_file.h"
 
 #include <fcntl.h>
+#include <map>
+#include <stdio.h>
 #include <string.h>
+#include <sys/file.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <map>
 
 #include "globals.h"
 #include "logging.h"
 #include "object.h"
 #include "scoped_ptr.h"
+#include "stringprintf.h"
+#include "thread.h"
 #include "utils.h"
+#include "zip_archive.h"
 
 namespace art {
 
@@ -34,9 +39,8 @@
 DexFile::PtrCloser::PtrCloser(byte* addr) : addr_(addr) {}
 DexFile::PtrCloser::~PtrCloser() { delete[] addr_; }
 
-DexFile* DexFile::OpenFile(const char* filename) {
-  CHECK(filename != NULL);
-  int fd = open(filename, O_RDONLY);  // TODO: scoped_fd
+DexFile* DexFile::OpenFile(const std::string& filename) {
+  int fd = open(filename.c_str(), O_RDONLY);  // TODO: scoped_fd
   if (fd == -1) {
     PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed";
     return NULL;
@@ -61,6 +65,199 @@
   return Open(dex_file, length, closer);
 }
 
+static const char* kClassesDex = "classes.dex";
+
+class LockedFd {
+ public:
+   static LockedFd* CreateAndLock(std::string& name, mode_t mode) {
+    int fd = open(name.c_str(), O_CREAT | O_RDWR, mode);
+    if (fd == -1) {
+      PLOG(ERROR) << "Can't open file '" << name;
+      return NULL;
+    }
+    fchmod(fd, mode);
+
+    LOG(INFO) << "locking file " << name << " (fd=" << fd << ")";
+    int result = flock(fd, LOCK_EX | LOCK_NB);
+    if (result == -1) {
+        LOG(WARNING) << "sleeping while locking file " << name;
+        result = flock(fd, LOCK_EX);
+    }
+    if (result == -1 ) {
+      close(fd);
+      PLOG(ERROR) << "Can't lock file '" << name;
+      return NULL;
+    }
+    return new LockedFd(fd);
+   }
+
+   int GetFd() const {
+     return fd_;
+   }
+
+  ~LockedFd() {
+    if (fd_ != -1) {
+      int result = flock(fd_, LOCK_UN);
+      if (result == -1) {
+        PLOG(WARNING) << "flock(" << fd_ << ", LOCK_UN) failed";
+      }
+      close(fd_);
+    }
+  }
+
+ private:
+  LockedFd(int fd) : fd_(fd) {}
+
+  int fd_;
+};
+
+class TmpFile {
+ public:
+  TmpFile(const std::string name) : name_(name) {}
+  ~TmpFile() {
+    unlink(name_.c_str());
+  }
+ private:
+  const std::string name_;
+};
+
+// Open classes.dex from within a .zip, .jar, .apk, ...
+DexFile* DexFile::OpenZip(const std::string& filename) {
+
+  // First, look for a ".dex" alongside the jar file.  It will have
+  // the same name/path except for the extension.
+
+  // Example filename = dir/foo.jar
+  std::string adjacent_dex_filename(filename);
+  size_t found = adjacent_dex_filename.find_last_of(".");
+  if (found == std::string::npos) {
+    LOG(WARNING) << "No . in filename" << filename;
+  }
+  adjacent_dex_filename.replace(adjacent_dex_filename.begin() + found,
+                                adjacent_dex_filename.end(),
+                                ".dex");
+  // Example adjacent_dex_filename = dir/foo.dex
+  DexFile* adjacent_dex_file = DexFile::OpenFile(adjacent_dex_filename);
+  if (adjacent_dex_file != NULL) {
+      // We don't verify anything in this case, because we aren't in
+      // the cache and typically the file is in the readonly /system
+      // area, so if something is wrong, there is nothing we can do.
+      return adjacent_dex_file;
+  }
+
+  char resolved[PATH_MAX];
+  char* absolute_path = realpath(filename.c_str(), resolved);
+  if (absolute_path == NULL) {
+      LOG(WARNING) << "Could not create absolute path for " << filename
+                   << " when looking for classes.dex";
+      return NULL;
+  }
+  std::string cache_file(absolute_path+1); // skip leading slash
+  std::replace(cache_file.begin(), cache_file.end(), '/', '@');
+  cache_file.push_back('@');
+  cache_file.append(kClassesDex);
+  // Example cache_file = parent@dir@foo.jar@classes.dex
+
+  const char* data_root = getenv("ANDROID_DATA");
+  if (data_root == NULL) {
+      data_root = "/data";
+  }
+
+  std::string cache_path_tmp = StringPrintf("%s/art-cache/%s", data_root, cache_file.c_str());
+  // Example cache_path_tmp = /data/art-cache/parent@dir@foo.jar@classes.dex
+
+  scoped_ptr<ZipArchive> zip_archive(ZipArchive::Open(filename));
+  if (zip_archive == NULL) {
+    LOG(WARNING) << "Could not open " << filename << " when looking for classes.dex";
+    return NULL;
+  }
+  scoped_ptr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex));
+  if (zip_entry == NULL) {
+    LOG(WARNING) << "Could not find classes.dex within " << filename;
+    return NULL;
+  }
+
+  std::string cache_path = StringPrintf("%s.%08x", cache_path_tmp.c_str(), zip_entry->GetCrc32());
+  // Example cache_path = /data/art-cache/parent@dir@foo.jar@classes.dex.1a2b3c4d
+
+  while (true) {
+    DexFile* cached_dex_file = DexFile::OpenFile(cache_path);
+    if (cached_dex_file != NULL) {
+      return cached_dex_file;
+    }
+
+    // Try to open the temporary cache file, grabbing an exclusive
+    // lock. If somebody else is working on it, we'll block here until
+    // they complete.  Because we're waiting on an external resource,
+    // we go into native mode.
+    Thread* current_thread = Thread::Current();
+    Thread::State old = current_thread->GetState();
+    current_thread->SetState(Thread::kNative);
+    scoped_ptr<LockedFd> fd(LockedFd::CreateAndLock(cache_path_tmp, 0644));
+    current_thread->SetState(old);
+    if (fd == NULL) {
+      return NULL;
+    }
+
+    // Check to see if the fd we opened and locked matches the file in
+    // the filesystem.  If they don't, then somebody else unlinked
+    // ours and created a new file, and we need to use that one
+    // instead.  (If we caught them between the unlink and the create,
+    // we'll get an ENOENT from the file stat.)
+    struct stat fd_stat;
+    int fd_stat_result = fstat(fd->GetFd(), &fd_stat);
+    if (fd_stat_result == -1) {
+      PLOG(ERROR) << "Can't stat open file '" << cache_path_tmp << "'";
+      return NULL;
+    }
+    struct stat file_stat;
+    int file_stat_result = stat(cache_path_tmp.c_str(), &file_stat);
+    if (file_stat_result == -1 ||
+        fd_stat.st_dev != file_stat.st_dev || fd_stat.st_ino != file_stat.st_ino) {
+      LOG(WARNING) << "our open cache file is stale; sleeping and retrying";
+      usleep(250 * 1000);  // if something is hosed, don't peg machine
+      continue;
+    }
+
+    // We have the correct file open and locked. Extract classes.dex
+    TmpFile tmp_file(cache_path_tmp);
+    bool success = zip_entry->Extract(fd->GetFd());
+    if (!success) {
+      return NULL;
+    }
+
+    // TODO restat and check length against zip_entry->GetUncompressedLength()?
+
+    // Compute checksum and compare to zip. If things look okay, rename from tmp.
+    off_t lseek_result = lseek(fd->GetFd(), 0, SEEK_SET);
+    if (lseek_result == -1) {
+      return NULL;
+    }
+    const size_t kBufSize = 32768;
+    scoped_ptr<uint8_t> buf(new uint8_t[kBufSize]);
+    if (buf == NULL) {
+      return NULL;
+    }
+    uint32_t computed_crc = crc32(0L, Z_NULL, 0);
+    while (true) {
+      ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd->GetFd(), buf.get(), kBufSize));
+      if (bytes_read == 0) {
+        break;
+      }
+      computed_crc = crc32(computed_crc, buf.get(), bytes_read);
+    }
+    if (computed_crc != zip_entry->GetCrc32()) {
+      return NULL;
+    }
+    int rename_result = rename(cache_path_tmp.c_str(), cache_path.c_str());
+    if (rename_result == -1) {
+      PLOG(ERROR) << "Can't install dex cache file '" << cache_path << "' from '" << cache_path_tmp;
+      unlink(cache_path.c_str());
+    }
+  }
+  // NOTREACHED
+}
+
 DexFile* DexFile::OpenPtr(byte* ptr, size_t length) {
   CHECK(ptr != NULL);
   DexFile::Closer* closer = new PtrCloser(ptr);
diff --git a/src/dex_file.h b/src/dex_file.h
index c8f8afa..e8f415d 100644
--- a/src/dex_file.h
+++ b/src/dex_file.h
@@ -204,7 +204,10 @@
   };
 
   // Opens a .dex file from the file system.
-  static DexFile* OpenFile(const char* filename);
+  static DexFile* OpenFile(const std::string& filename);
+
+  // Opens a .jar, .zip, or .apk file from the file system.
+  static DexFile* OpenZip(const std::string& filename);
 
   // Opens a .dex file from a new allocated pointer
   static DexFile* OpenPtr(byte* ptr, size_t length);
diff --git a/src/globals.h b/src/globals.h
index b2f73fd..2826bbd 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -32,6 +32,8 @@
 const int kBitsPerWord = kWordSize * kBitsPerByte;
 const int kBitsPerInt = kIntSize * kBitsPerByte;
 
+const int kPageSize = 4096;
+
 }  // namespace art
 
 #endif  // ART_SRC_GLOBALS_H_
diff --git a/src/jni_compiler.cc b/src/jni_compiler.cc
index 5b57e71..8433c27 100644
--- a/src/jni_compiler.cc
+++ b/src/jni_compiler.cc
@@ -368,7 +368,7 @@
 JniCompiler::JniCompiler() {
   // TODO: this shouldn't be managed by the JniCompiler, we should have a
   // code cache.
-  jni_code_size_ = 4096;
+  jni_code_size_ = kPageSize;
   jni_code_ = static_cast<byte*>(mmap(NULL, jni_code_size_,
                                       PROT_READ | PROT_WRITE | PROT_EXEC,
                                       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));
diff --git a/src/jni_compiler_test.cc b/src/jni_compiler_test.cc
index ddf8f69..7dcfdc2 100644
--- a/src/jni_compiler_test.cc
+++ b/src/jni_compiler_test.cc
@@ -19,7 +19,7 @@
   virtual void SetUp() {
     RuntimeTest::SetUp();
     // Create thunk code that performs the native to managed transition
-    thunk_code_size_ = 4096;
+    thunk_code_size_ = kPageSize;
     thunk_ = mmap(NULL, thunk_code_size_, PROT_READ | PROT_WRITE | PROT_EXEC,
                   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
     CHECK_NE(MAP_FAILED, thunk_);
diff --git a/src/runtime.cc b/src/runtime.cc
index 5857c3a..6d0dfd2 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -126,6 +126,7 @@
 }
 
 bool Runtime::Init(const std::vector<DexFile*>& boot_class_path) {
+  CHECK_EQ(kPageSize, sysconf(_SC_PAGE_SIZE));
   thread_list_ = ThreadList::Create();
   Heap::Init(Heap::kStartupSize, Heap::kMaximumSize);
   Thread::Init();
diff --git a/src/runtime.h b/src/runtime.h
index 7a1a504..1a9661f 100644
--- a/src/runtime.h
+++ b/src/runtime.h
@@ -62,15 +62,15 @@
  private:
   static void PlatformAbort(const char*, int);
 
-  Runtime() : class_linker_(NULL), thread_list_(NULL) {}
+  Runtime() : thread_list_(NULL), class_linker_(NULL) {}
 
   // Initializes a new uninitialized runtime.
   bool Init(const std::vector<DexFile*>& boot_class_path);
 
-  ClassLinker* class_linker_;
-
   ThreadList* thread_list_;
 
+  ClassLinker* class_linker_;
+
   // A pointer to the active runtime or NULL.
   static Runtime* instance_;
 
diff --git a/src/space.cc b/src/space.cc
index a0da45c..217c2ee 100644
--- a/src/space.cc
+++ b/src/space.cc
@@ -46,7 +46,7 @@
   if (!(startup_size_ <= maximum_size_)) {
     return false;
   }
-  size_t length = RoundUp(maximum_size_, 4096);
+  size_t length = RoundUp(maximum_size_, kPageSize);
   int prot = PROT_READ | PROT_WRITE;
   int flags = MAP_PRIVATE | MAP_ANONYMOUS;
   void* base = mmap(NULL, length, prot, flags, -1, 0);
@@ -103,8 +103,8 @@
 }
 
 void Space::DontNeed(void* start, void* end, void* num_bytes) {
-  start = (void*)RoundUp((uintptr_t)start, 4096);
-  end = (void*)RoundDown((uintptr_t)end, 4096);
+  start = (void*)RoundUp((uintptr_t)start, kPageSize);
+  end = (void*)RoundDown((uintptr_t)end, kPageSize);
   if (start >= end) {
     return;
   }
diff --git a/src/thread.cc b/src/thread.cc
index c2483c0..68f4566 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -88,7 +88,7 @@
   thread->InitCpu();
   thread->stack_limit_ = reinterpret_cast<byte*>(-1);  // TODO: getrlimit
   uintptr_t addr = reinterpret_cast<uintptr_t>(&thread);  // TODO: ask pthreads
-  uintptr_t stack_base = RoundUp(addr, 4096);
+  uintptr_t stack_base = RoundUp(addr, kPageSize);
   thread->stack_base_ = reinterpret_cast<byte*>(stack_base);
   // TODO: set the stack size
 
diff --git a/src/thread_x86.cc b/src/thread_x86.cc
index d0a4469..0e7cd7d 100644
--- a/src/thread_x86.cc
+++ b/src/thread_x86.cc
@@ -19,7 +19,7 @@
   struct user_desc ldt_entry;
   ldt_entry.entry_number = -1;
   ldt_entry.base_addr = (unsigned int)this;
-  ldt_entry.limit = 4096;
+  ldt_entry.limit = kPageSize;
   ldt_entry.seg_32bit = 1;
   ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
   ldt_entry.read_exec_only = 0;
diff --git a/src/zip_archive.cc b/src/zip_archive.cc
new file mode 100644
index 0000000..09dc3d4
--- /dev/null
+++ b/src/zip_archive.cc
@@ -0,0 +1,472 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "zip_archive.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+namespace art {
+
+// Get 2 little-endian bytes.
+static uint32_t Le16ToHost(const uint8_t* src) {
+  return ((src[0] <<  0) |
+          (src[1] <<  8));
+}
+
+// Get 4 little-endian bytes.
+static uint32_t Le32ToHost(const uint8_t* src) {
+  return ((src[0] <<  0) |
+          (src[1] <<  8) |
+          (src[2] << 16) |
+          (src[3] << 24));
+}
+
+uint16_t ZipEntry::GetCompressionMethod() {
+  return Le16ToHost(ptr_ + ZipArchive::kCDEMethod);
+}
+
+uint32_t ZipEntry::GetCompressedLength() {
+  return Le32ToHost(ptr_ + ZipArchive::kCDECompLen);
+}
+
+uint32_t ZipEntry::GetUncompressedLength() {
+  return Le32ToHost(ptr_ + ZipArchive::kCDEUncompLen);
+}
+
+uint32_t ZipEntry::GetCrc32() {
+  return Le32ToHost(ptr_ + ZipArchive::kCDECRC);
+}
+
+off_t ZipEntry::GetDataOffset() {
+  // All we have is the offset to the Local File Header, which is
+  // variable size, so we have to read the contents of the struct to
+  // figure out where the actual data starts.
+
+  // We also need to make sure that the lengths are not so large that
+  // somebody trying to map the compressed or uncompressed data runs
+  // off the end of the mapped region.
+
+  off_t dir_offset = zip_archive_->dir_offset_;
+  int64_t local_hdr_offset = Le32ToHost(ptr_ + ZipArchive::kCDELocalOffset);
+  if (local_hdr_offset + ZipArchive::kLFHLen >= dir_offset) {
+    LOG(WARNING) << "Zip: bad local hdr offset in zip";
+    return -1;
+  }
+
+  if (lseek(zip_archive_->fd_, local_hdr_offset, SEEK_SET) != local_hdr_offset) {
+    PLOG(WARNING) << "Zip: failed seeking to lfh at offset " << local_hdr_offset;
+    return -1;
+  }
+
+  uint8_t lfh_buf[ZipArchive::kLFHLen];
+  ssize_t actual = TEMP_FAILURE_RETRY(read(zip_archive_->fd_, lfh_buf, sizeof(lfh_buf)));
+  if (actual != sizeof(lfh_buf)) {
+    LOG(WARNING) << "Zip: failed reading lfh from offset " << local_hdr_offset;
+    return -1;
+  }
+
+  if (Le32ToHost(lfh_buf) != ZipArchive::kLFHSignature) {
+    LOG(WARNING) << "Zip: didn't find signature at start of lfh, offset " << local_hdr_offset;
+    return -1;
+  }
+
+  off_t data_offset = (local_hdr_offset + ZipArchive::kLFHLen
+                       + Le16ToHost(lfh_buf + ZipArchive::kLFHNameLen)
+                       + Le16ToHost(lfh_buf + ZipArchive::kLFHExtraLen));
+  if (data_offset >= dir_offset) {
+    LOG(WARNING) << "Zip: bad data offset " << data_offset << " in zip";
+    return -1;
+  }
+
+  // check lengths
+
+  if (static_cast<off_t>(data_offset + GetCompressedLength()) > dir_offset) {
+    LOG(WARNING) << "Zip: bad compressed length in zip "
+                 << "(" << data_offset << " + " << GetCompressedLength()
+                 << " > " << dir_offset << ")";
+    return -1;
+  }
+
+  if (GetCompressionMethod() == kCompressStored
+      && static_cast<off_t>(data_offset + GetUncompressedLength()) > dir_offset) {
+    LOG(WARNING) << "Zip: bad uncompressed length in zip "
+                 << "(" << data_offset << " + " << GetUncompressedLength()
+                 << " > " << dir_offset << ")";
+    return -1;
+  }
+
+  return data_offset;
+}
+
+// Write until all bytes have been written, returning true on success
+bool WriteFully(int fd, const uint8_t* buf, size_t count) {
+  while (count != 0) {
+    ssize_t actual = TEMP_FAILURE_RETRY(write(fd, buf, count));
+    if (actual < 0) {
+      return false;
+    }
+    if (actual != static_cast<ssize_t>(count)) {
+      buf += actual;
+    }
+    count -= actual;
+  }
+  return true;
+}
+
+static bool CopyFdToFd(int out, int in, size_t count) {
+  const size_t kBufSize = 32768;
+  uint8_t buf[kBufSize];
+
+  while (count != 0) {
+    size_t bytes_to_read = (count > kBufSize) ? kBufSize : count;
+    ssize_t actual = TEMP_FAILURE_RETRY(read(in, buf, bytes_to_read));
+    if (actual != static_cast<ssize_t>(bytes_to_read)) {
+      return false;
+    }
+    if (!WriteFully(out, buf, bytes_to_read)) {
+      return false;
+    }
+    count -= bytes_to_read;
+  }
+  return true;
+}
+
+class ZStream {
+ public:
+  ZStream(uint8_t* write_buf, size_t write_buf_size) {
+    // Initialize the zlib stream struct.
+    memset(&zstream_, 0, sizeof(zstream_));
+    zstream_.zalloc = Z_NULL;
+    zstream_.zfree = Z_NULL;
+    zstream_.opaque = Z_NULL;
+    zstream_.next_in = NULL;
+    zstream_.avail_in = 0;
+    zstream_.next_out = reinterpret_cast<Bytef*>(write_buf);
+    zstream_.avail_out = write_buf_size;
+    zstream_.data_type = Z_UNKNOWN;
+  }
+
+  z_stream& Get() {
+    return zstream_;
+  }
+
+  ~ZStream() {
+    inflateEnd(&zstream_);
+  }
+ private:
+  z_stream zstream_;
+};
+
+static bool InflateToFd(int out, int in, size_t uncompressed_length, size_t compressed_length) {
+  const size_t kBufSize = 32768;
+  scoped_ptr<uint8_t> read_buf(new uint8_t[kBufSize]);
+  scoped_ptr<uint8_t> write_buf(new uint8_t[kBufSize]);
+  if (read_buf == NULL || write_buf == NULL) {
+    return false;
+  }
+
+  scoped_ptr<ZStream> zstream(new ZStream(write_buf.get(), kBufSize));
+
+  // Use the undocumented "negative window bits" feature to tell zlib
+  // that there's no zlib header waiting for it.
+  int zerr = inflateInit2(&zstream->Get(), -MAX_WBITS);
+  if (zerr != Z_OK) {
+    if (zerr == Z_VERSION_ERROR) {
+      LOG(ERROR) << "Installed zlib is not compatible with linked version (" << ZLIB_VERSION << ")";
+    } else {
+      LOG(WARNING) << "Call to inflateInit2 failed (zerr=" << zerr << ")";
+    }
+    return false;
+  }
+
+  size_t remaining = compressed_length;
+  do {
+    // read as much as we can
+    if (zstream->Get().avail_in == 0) {
+      size_t bytes_to_read = (remaining > kBufSize) ? kBufSize : remaining;
+
+        ssize_t actual = TEMP_FAILURE_RETRY(read(in, read_buf.get(), bytes_to_read));
+        if (actual != static_cast<ssize_t>(bytes_to_read)) {
+          LOG(WARNING) << "Zip: inflate read failed (" << actual << " vs " << bytes_to_read << ")";
+          return false;
+        }
+        remaining -= bytes_to_read;
+        zstream->Get().next_in = read_buf.get();
+        zstream->Get().avail_in = bytes_to_read;
+    }
+
+    // uncompress the data
+    zerr = inflate(&zstream->Get(), Z_NO_FLUSH);
+    if (zerr != Z_OK && zerr != Z_STREAM_END) {
+      LOG(WARNING) << "Zip: inflate zerr=" << zerr
+                   << " (nIn=" << zstream->Get().next_in
+                   << " aIn=" << zstream->Get().avail_in
+                   << " nOut=" << zstream->Get().next_out
+                   << " aOut=" << zstream->Get().avail_out
+                   << ")";
+      return false;
+    }
+
+    // write when we're full or when we're done
+    if (zstream->Get().avail_out == 0 ||
+        (zerr == Z_STREAM_END && zstream->Get().avail_out != kBufSize)) {
+      size_t bytes_to_write = zstream->Get().next_out - write_buf.get();
+      if (!WriteFully(out, write_buf.get(), bytes_to_write)) {
+        return false;
+      }
+      zstream->Get().next_out = write_buf.get();
+      zstream->Get().avail_out = kBufSize;
+    }
+  } while (zerr == Z_OK);
+
+  DCHECK(zerr == Z_STREAM_END); // other errors should've been caught
+
+  // paranoia
+  if (zstream->Get().total_out != uncompressed_length) {
+    LOG(WARNING) << "Zip: size mismatch on inflated file ("
+                 << zstream->Get().total_out << " vs " << uncompressed_length << ")";
+    return false;
+  }
+
+  return true;
+}
+
+bool ZipEntry::Extract(int fd) {
+
+  off_t data_offset = GetDataOffset();
+  if (data_offset == -1) {
+    return false;
+  }
+  if (lseek(zip_archive_->fd_, data_offset, SEEK_SET) != data_offset) {
+    PLOG(WARNING) << "Zip: lseek to data at " << data_offset << " failed";
+    return false;
+  }
+
+  // TODO: this doesn't verify the data's CRC, but probably should (especially
+  // for uncompressed data).
+  switch (GetCompressionMethod()) {
+    case kCompressStored:
+      return CopyFdToFd(fd, zip_archive_->fd_, GetUncompressedLength());
+    case kCompressDeflated:
+      return InflateToFd(fd, zip_archive_->fd_, GetUncompressedLength(), GetCompressedLength());
+    default:
+      return false;
+  }
+}
+
+static bool CloseOnExec(int fd) {
+  int flags = fcntl(fd, F_GETFD);
+  if (flags < 0) {
+    return false;
+  }
+  if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
+    return false;
+  }
+  return true;
+}
+
+// return new ZipArchive instance on success, NULL on error.
+ZipArchive* ZipArchive::Open(const std::string& filename) {
+  DCHECK(!filename.empty());
+  int fd = open(filename.c_str(), O_RDONLY, 0);
+  if (fd < 0) {
+    int err = errno ? errno : -1;
+    LOG(WARNING) << "Unable to open '" << filename << "': " << strerror(err);
+    return NULL;
+  }
+  if (!CloseOnExec(fd)) {
+    return NULL;
+  }
+  scoped_ptr<ZipArchive> zip_archive(new ZipArchive(fd));
+  if (zip_archive == NULL) {
+      return NULL;
+  }
+  if (!zip_archive->MapCentralDirectory()) {
+      zip_archive->Close();
+      return NULL;
+  }
+  if (!zip_archive->Parse()) {
+      zip_archive->Close();
+      return NULL;
+  }
+  return zip_archive.release();
+}
+
+ZipEntry* ZipArchive::Find(const char* name) {
+  DCHECK(name != NULL);
+  std::map<StringPiece, const uint8_t*>::const_iterator it = dir_entries_.find(name);
+  if (it == dir_entries_.end()) {
+    return NULL;
+  }
+  return new ZipEntry(this, (*it).second);
+}
+
+void ZipArchive::Close() {
+  if (fd_ != -1) {
+    close(fd_);
+  }
+  fd_ = -1;
+  num_entries_ = 0;
+  dir_offset_ = 0;
+}
+
+// Find the zip Central Directory and memory-map it.
+//
+// On success, returns true after populating fields from the EOCD area:
+//   num_entries_
+//   dir_offset_
+//   dir_map_
+bool ZipArchive::MapCentralDirectory() {
+  /*
+   * Get and test file length.
+   */
+  off_t file_length = lseek(fd_, 0, SEEK_END);
+  if (file_length < kEOCDLen) {
+      LOG(WARNING) << "Zip: length " << file_length << " is too small to be zip";
+      return false;
+  }
+
+  // Perform the traditional EOCD snipe hunt.
+  //
+  // We're searching for the End of Central Directory magic number,
+  // which appears at the start of the EOCD block.  It's followed by
+  // 18 bytes of EOCD stuff and up to 64KB of archive comment.  We
+  // need to read the last part of the file into a buffer, dig through
+  // it to find the magic number, parse some values out, and use those
+  // to determine the extent of the CD.
+  //
+  // We start by pulling in the last part of the file.
+  size_t read_amount = kMaxEOCDSearch;
+  if (file_length < off_t(read_amount)) {
+      read_amount = file_length;
+  }
+
+  scoped_ptr<uint8_t> scan_buf(new uint8_t[read_amount]);
+  if (scan_buf == NULL) {
+    return false;
+  }
+
+  off_t search_start = file_length - read_amount;
+
+  if (lseek(fd_, search_start, SEEK_SET) != search_start) {
+    LOG(WARNING) << "Zip: seek " << search_start << " failed: " << strerror(errno);
+    return false;
+  }
+  ssize_t actual = TEMP_FAILURE_RETRY(read(fd_, scan_buf.get(), read_amount));
+  if (actual == -1) {
+    LOG(WARNING) << "Zip: read " << read_amount << " failed: " << strerror(errno);
+    return false;
+  }
+
+
+  // Scan backward for the EOCD magic.  In an archive without a trailing
+  // comment, we'll find it on the first try.  (We may want to consider
+  // doing an initial minimal read; if we don't find it, retry with a
+  // second read as above.)
+  int i;
+  for (i = read_amount - kEOCDLen; i >= 0; i--) {
+    if (scan_buf.get()[i] == 0x50 && Le32ToHost(&(scan_buf.get())[i]) == kEOCDSignature) {
+      break;
+    }
+  }
+  if (i < 0) {
+    LOG(WARNING) << "Zip: EOCD not found, not a zip file";
+    return false;
+  }
+
+  off_t eocd_offset = search_start + i;
+  const uint8_t* eocd_ptr = scan_buf.get() + i;
+
+  DCHECK(eocd_offset < file_length);
+
+  // Grab the CD offset and size, and the number of entries in the
+  // archive.  Verify that they look reasonable.
+  uint16_t num_entries = Le16ToHost(eocd_ptr + kEOCDNumEntries);
+  uint32_t dir_size = Le32ToHost(eocd_ptr + kEOCDSize);
+  uint32_t dir_offset = Le32ToHost(eocd_ptr + kEOCDFileOffset);
+
+  if ((uint64_t) dir_offset + (uint64_t) dir_size > (uint64_t) eocd_offset) {
+    LOG(WARNING) << "Zip: bad offsets ("
+                 << "dir=" << dir_offset << ", "
+                 << "size=" << dir_size  << ", "
+                 << "eocd=" << eocd_offset << ")";
+    return false;
+  }
+  if (num_entries == 0) {
+    LOG(WARNING) << "Zip: empty archive?";
+    return false;
+  }
+
+  // It all looks good.  Create a mapping for the CD.
+  dir_map_.reset(MemMap::Map(fd_, dir_offset, dir_size));
+  if (dir_map_ == NULL) {
+    LOG(WARNING) << "Zip: cd map failed " << strerror(errno);
+    return false;
+  }
+
+  num_entries_ = num_entries;
+  dir_offset_ = dir_offset;
+  return true;
+}
+
+bool ZipArchive::Parse() {
+  const uint8_t* cd_ptr = reinterpret_cast<const uint8_t*>(dir_map_->GetAddress());
+  size_t cd_length = dir_map_->GetLength();
+
+  // Walk through the central directory, adding entries to the hash
+  // table and verifying values.
+  const uint8_t* ptr = cd_ptr;
+  for (int i = 0; i < num_entries_; i++) {
+    if (Le32ToHost(ptr) != kCDESignature) {
+      LOG(WARNING) << "Zip: missed a central dir sig (at " << i << ")";
+      return false;
+    }
+    if (ptr + kCDELen > cd_ptr + cd_length) {
+      LOG(WARNING) << "Zip: ran off the end (at " << i << ")";
+      return false;
+    }
+
+    int64_t local_hdr_offset = Le32ToHost(ptr + kCDELocalOffset);
+    if (local_hdr_offset >= dir_offset_) {
+      LOG(WARNING) << "Zip: bad LFH offset " << local_hdr_offset << " at entry " << i;
+      return false;
+    }
+
+    uint16_t filename_len = Le16ToHost(ptr + kCDENameLen);
+    uint16_t extra_len = Le16ToHost(ptr + kCDEExtraLen);
+    uint16_t comment_len = Le16ToHost(ptr + kCDECommentLen);
+
+    // add the CDE filename to the hash table
+    const char* name = reinterpret_cast<const char*>(ptr + kCDELen);
+    bool success = dir_entries_.insert(std::make_pair(StringPiece(name, filename_len), ptr)).second;
+    if (!success) {
+        return false;
+    }
+    ptr += kCDELen + filename_len + extra_len + comment_len;
+    if (ptr > cd_ptr + cd_length) {
+      LOG(WARNING) << "Zip: bad CD advance "
+                   << "(" << ptr << " vs " << (cd_ptr + cd_length) << ") "
+                   << "at entry " << i;
+      return false;
+    }
+  }
+  return true;
+}
+
+}  // namespace art
diff --git a/src/zip_archive.h b/src/zip_archive.h
new file mode 100644
index 0000000..9eb2948
--- /dev/null
+++ b/src/zip_archive.h
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_SRC_ZIP_ARCHIVE_H_
+#define ART_SRC_ZIP_ARCHIVE_H_
+
+#include <map>
+#include <stdint.h>
+#include <sys/mman.h>
+#include <zlib.h>
+
+#include "globals.h"
+#include "logging.h"
+#include "scoped_ptr.h"
+#include "stringpiece.h"
+
+namespace art {
+
+class ZipArchive;
+class MemMap;
+
+class ZipEntry {
+
+ public:
+  // Uncompress an entry, in its entirety, to an open file descriptor.
+  bool Extract(int fd);
+
+  uint32_t GetCrc32();
+
+ private:
+
+  ZipEntry(ZipArchive* zip_archive, const uint8_t* ptr) : zip_archive_(zip_archive), ptr_(ptr) {};
+
+  // Zip compression methods
+  enum {
+    kCompressStored     = 0,        // no compression
+    kCompressDeflated   = 8,        // standard deflate
+  };
+
+  // kCompressStored, kCompressDeflated, ...
+  uint16_t GetCompressionMethod();
+
+  uint32_t GetCompressedLength();
+
+  uint32_t GetUncompressedLength();
+
+  // returns -1 on error
+  off_t GetDataOffset();
+
+  ZipArchive* zip_archive_;
+
+  // pointer to zip entry within central directory
+  const uint8_t* ptr_;
+
+  friend class ZipArchive;
+};
+
+// Used to keep track of unaligned mmap segments.
+class MemMap {
+ public:
+
+  // Map part of a file into a shared, read-only memory segment.  The "start"
+  // offset is absolute, not relative.
+  //
+  // On success, returns returns a MemMap instance.  On failure, returns a NULL;
+  static MemMap* Map(int fd, off_t start, size_t length) {
+    // adjust to be page-aligned
+    int page_offset = start % kPageSize;
+    off_t page_aligned_offset = start - page_offset;
+    size_t page_aligned_size = length + page_offset;
+    uint8_t* addr = reinterpret_cast<uint8_t*>(mmap(NULL,
+                                                    page_aligned_size,
+                                                    PROT_READ,
+                                                    MAP_FILE | MAP_SHARED,
+                                                    fd,
+                                                    page_aligned_offset));
+    if (addr == MAP_FAILED) {
+      return NULL;
+    }
+    return new MemMap(addr+page_offset, length, addr, page_aligned_size);
+  }
+
+  ~MemMap() {
+    Unmap();
+  };
+
+  // Release a memory mapping, returning true on success or it was previously unmapped.
+  bool Unmap() {
+    if (base_addr_ == NULL && base_length_ == 0) {
+      return true;
+    }
+    int result = munmap(base_addr_, base_length_);
+    if (result != 0) {
+      return false;
+    }
+    base_addr_ = NULL;
+    base_length_ = 0;
+    return true;
+  }
+
+  void* GetAddress() {
+    return addr_;
+  }
+
+  size_t GetLength() {
+    return length_;
+  }
+
+ private:
+  MemMap(void* addr, size_t length, void* base_addr, size_t base_length)
+      : addr_(addr), length_(length), base_addr_(base_addr), base_length_(base_length) {
+    CHECK(addr_ != NULL);
+    CHECK(length_ != 0);
+    CHECK(base_addr_ != NULL);
+    CHECK(base_length_ != 0);
+  };
+
+  void*   addr_;              // start of data
+  size_t  length_;            // length of data
+
+  void*   base_addr_;         // page-aligned base address
+  size_t  base_length_;       // length of mapping
+};
+
+class ZipArchive {
+ public:
+
+  // Zip file constants.
+  static const uint32_t kEOCDSignature  = 0x06054b50;
+  static const int32_t kEOCDLen         = 22;
+  static const int32_t kEOCDNumEntries  =  8;  // offset to #of entries in file
+  static const int32_t kEOCDSize        = 12;  // size of the central directory
+  static const int32_t kEOCDFileOffset  = 16;  // offset to central directory
+
+  static const int32_t kMaxCommentLen = 65535;  // longest possible in uint16_t
+  static const int32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen);
+
+  static const uint32_t kLFHSignature = 0x04034b50;
+  static const int32_t kLFHLen        = 30;  // excluding variable-len fields
+  static const int32_t kLFHNameLen    = 26;  // offset to filename length
+  static const int32_t kLFHExtraLen   = 28;  // offset to extra length
+
+  static const uint32_t kCDESignature   = 0x02014b50;
+  static const int32_t kCDELen          = 46;  // excluding variable-len fields
+  static const int32_t kCDEMethod       = 10;  // offset to compression method
+  static const int32_t kCDEModWhen      = 12;  // offset to modification timestamp
+  static const int32_t kCDECRC          = 16;  // offset to entry CRC
+  static const int32_t kCDECompLen      = 20;  // offset to compressed length
+  static const int32_t kCDEUncompLen    = 24;  // offset to uncompressed length
+  static const int32_t kCDENameLen      = 28;  // offset to filename length
+  static const int32_t kCDEExtraLen     = 30;  // offset to extra length
+  static const int32_t kCDECommentLen   = 32;  // offset to comment length
+  static const int32_t kCDELocalOffset  = 42;  // offset to local hdr
+
+  static ZipArchive* Open(const std::string& filename);
+  ZipEntry* Find(const char * name);
+
+  ~ZipArchive() {
+    Close();
+  }
+
+ private:
+  ZipArchive(int fd) : fd_(fd), num_entries_(0), dir_offset_(0) {}
+
+  bool MapCentralDirectory();
+  bool Parse();
+  void Close();
+
+  int fd_;
+  uint16_t num_entries_;
+  off_t dir_offset_;
+  scoped_ptr<MemMap> dir_map_;
+  // TODO: unordered map
+  std::map<StringPiece, const uint8_t*> dir_entries_;
+
+  friend class ZipEntry;
+};
+
+}  // namespace art
+
+#endif  // ART_SRC_ZIP_ARCHIVE_H_
diff --git a/src/zip_archive_test.cc b/src/zip_archive_test.cc
new file mode 100644
index 0000000..472d5eb
--- /dev/null
+++ b/src/zip_archive_test.cc
@@ -0,0 +1,71 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "common_test.h"
+#include "zip_archive.h"
+#include "gtest/gtest.h"
+
+namespace art {
+
+class ZipArchiveTest : public RuntimeTest {};
+
+class TmpFile {
+ public:
+  TmpFile() {
+    filename_.reset(strdup("TmpFile-XXXXXX"));
+    CHECK(filename_ != NULL);
+    fd_ = mkstemp(filename_.get());
+    CHECK_NE(-1, fd_);
+  }
+
+  ~TmpFile() {
+    int unlink_result = unlink(filename_.get());
+    CHECK_EQ(0, unlink_result);
+    int close_result = close(fd_);
+    CHECK_EQ(0, close_result);
+  }
+
+  const char* GetFilename() const {
+    return filename_.get();
+  }
+
+  int GetFd() const {
+    return fd_;
+  }
+
+ private:
+  scoped_ptr_malloc<char> filename_;
+  int fd_;
+};
+
+TEST_F(ZipArchiveTest, FindAndExtract) {
+  scoped_ptr<ZipArchive> zip_archive(ZipArchive::Open(GetLibCoreDexFileName()));
+  ASSERT_TRUE(zip_archive != false);
+  scoped_ptr<ZipEntry> zip_entry(zip_archive->Find("classes.dex"));
+  ASSERT_TRUE(zip_entry != false);
+
+  TmpFile tmp;
+  ASSERT_NE(-1, tmp.GetFd());
+  bool success = zip_entry->Extract(tmp.GetFd());
+  ASSERT_TRUE(success);
+  close(tmp.GetFd());
+
+  uint32_t computed_crc = crc32(0L, Z_NULL, 0);
+  int fd = open(tmp.GetFilename(), O_RDONLY);
+  ASSERT_NE(-1, fd);
+  const size_t kBufSize = 32768;
+  uint8_t buf[kBufSize];
+  while (true) {
+    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buf, kBufSize));
+    if (bytes_read == 0) {
+      break;
+    }
+    computed_crc = crc32(computed_crc, buf, bytes_read);
+  }
+  EXPECT_EQ(zip_entry->GetCrc32(), computed_crc);
+}
+
+}  // namespace art