Fix ExtractEntryToFile.

We would always write uncompressed data at offset 0 instead
of the current filedescriptor offset.

Also adds a unit-test & a clarifying comment on the API.

Change-Id: If44757e96dde504ce63d81b4dec7115fc6f6d5fb
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index dddff1f..2b827b3 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -1007,13 +1007,21 @@
                            ZipEntry* entry, int fd) {
   const int32_t declared_length = entry->uncompressed_length;
 
-  int result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length));
-  if (result == -1) {
-    ALOGW("Zip: unable to truncate file to %ud", declared_length);
+  const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
+  if (current_offset == -1) {
+    ALOGW("Zip: unable to seek to current location on fd %d: %s", fd,
+          strerror(errno));
     return kIoError;
   }
 
-  android::FileMap* map  = MapFileSegment(fd, 0, declared_length,
+  int result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
+  if (result == -1) {
+    ALOGW("Zip: unable to truncate file to %lld: %s", declared_length + current_offset,
+          strerror(errno));
+    return kIoError;
+  }
+
+  android::FileMap* map  = MapFileSegment(fd, current_offset, declared_length,
                                           false, kTempMappingFileName);
   if (map == NULL) {
     return kMmapFailed;