diff --git a/src/apk_archive.h b/src/apk_archive.h
index d3b787db5afcce009f697063acb7e9e99808b90e..f1787dc50740c4d35ec76a9982252743a27068b7 100644
--- a/src/apk_archive.h
+++ b/src/apk_archive.h
@@ -21,7 +21,6 @@ typedef int (*apk_archive_entry_parser)(void *ctx,
 					struct apk_istream *istream);
 
 int apk_parse_tar(struct apk_istream *, apk_archive_entry_parser parser, void *ctx);
-int apk_parse_tar_gz(struct apk_bstream *, apk_archive_entry_parser parser, void *ctx);
 int apk_write_tar_entry(struct apk_ostream *, const struct apk_file_info *ae, char *data);
 
 int apk_archive_entry_extract(const struct apk_file_info *ae,
diff --git a/src/apk_io.h b/src/apk_io.h
index 1fe4f2c5c4d269d658050b3595274101eb527ce5..49d9fcfa3529d89d95e0811b934f6842285ca1f2 100644
--- a/src/apk_io.h
+++ b/src/apk_io.h
@@ -51,12 +51,11 @@ struct apk_ostream {
 
 typedef int (*apk_multipart_cb)(void *ctx, EVP_MD_CTX *mdctx, int part);
 
-struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *, int,
+struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *,
 					     apk_multipart_cb cb, void *ctx);
-static inline struct apk_istream *apk_bstream_gunzip(struct apk_bstream *bs,
-						     int autoclose)
+static inline struct apk_istream *apk_bstream_gunzip(struct apk_bstream *bs)
 {
-	return apk_bstream_gunzip_mpart(bs, autoclose, NULL, NULL);
+	return apk_bstream_gunzip_mpart(bs, NULL, NULL);
 }
 
 struct apk_ostream *apk_ostream_gzip(struct apk_ostream *);
diff --git a/src/archive.c b/src/archive.c
index 0a53792cf15cab06819380a3d6c5c6a2ab86a240..8e55295b5e6506be090bd0a1517bc4483e98574e 100644
--- a/src/archive.c
+++ b/src/archive.c
@@ -255,19 +255,6 @@ int apk_write_tar_entry(struct apk_ostream *os, const struct apk_file_info *ae,
 	return 0;
 }
 
-int apk_parse_tar_gz(struct apk_bstream *bs, apk_archive_entry_parser parser,
-		     void *ctx)
-{
-	struct apk_istream *is;
-	int rc;
-
-	is = apk_bstream_gunzip(bs, FALSE);
-	rc = apk_parse_tar(is, parser, ctx);
-	is->close(is);
-
-	return rc;
-}
-
 int apk_archive_entry_extract(const struct apk_file_info *ae,
 			      struct apk_istream *is,
 			      const char *fn, apk_progress_cb cb,
diff --git a/src/database.c b/src/database.c
index 4daa66a6e484b37a29f6b4566746ba3af3bcfcd5..2a15385a15c660acff1357a310b3c5a256dac15c 100644
--- a/src/database.c
+++ b/src/database.c
@@ -1135,7 +1135,7 @@ int apk_db_add_repository(apk_database_t _db, apk_blob_t repository)
 	} else {
 		bs = apk_repository_file_open(repo, apk_index_gz);
 	}
-	bs = apk_bstream_from_istream(apk_bstream_gunzip(bs, TRUE));
+	bs = apk_bstream_from_istream(apk_bstream_gunzip(bs));
 	if (bs == NULL) {
 		apk_warning("Failed to open index for %s", repo->url);
 		return -1;
@@ -1377,7 +1377,6 @@ static int apk_db_unpack_pkg(struct apk_database *db,
 	struct apk_istream *tar;
 	char pkgname[256], file[256];
 	int i, need_copy = FALSE;
-	size_t length;
 
 	snprintf(pkgname, sizeof(pkgname), "%s-%s.apk",
 		 newpkg->name->name, newpkg->version);
@@ -1433,10 +1432,10 @@ static int apk_db_unpack_pkg(struct apk_database *db,
 		.cb_ctx = cb_ctx,
 	};
 
-	tar = apk_bstream_gunzip_mpart(bs, FALSE, apk_db_gzip_part, &ctx);
+	tar = apk_bstream_gunzip_mpart(bs, apk_db_gzip_part, &ctx);
 	if (apk_parse_tar(tar, apk_db_install_archive_entry, &ctx) != 0)
 		goto err_close;
-	bs->close(bs, &length);
+	tar->close(tar);
 
 	/* Check the package checksum */
 	if (apk_checksum_compare(&ctx.data_csum, &newpkg->csum) != 0)
@@ -1444,14 +1443,10 @@ static int apk_db_unpack_pkg(struct apk_database *db,
 			    newpkg->name->name, newpkg->version);
 
 	if (need_copy) {
-		if (length == newpkg->size) {
-			char file2[256];
-			apk_db_cache_get_name(file2, sizeof(file2), db,
-					      &newpkg->csum, pkgname, FALSE);
-			rename(file, file2);
-		} else {
-			unlink(file);
-		}
+		char file2[256];
+		apk_db_cache_get_name(file2, sizeof(file2), db,
+				      &newpkg->csum, pkgname, FALSE);
+		rename(file, file2);
 	}
 
 	return 0;
diff --git a/src/gunzip.c b/src/gunzip.c
index 010d45ef3786ab67dfd35eda453a7ed5efeba48a..af906d1e516a7938dc34a19003ded18753278c26 100644
--- a/src/gunzip.c
+++ b/src/gunzip.c
@@ -22,7 +22,6 @@ struct apk_gzip_istream {
 	struct apk_bstream *bs;
 	z_stream zs;
 	int z_err;
-	int autoclose;
 
 	EVP_MD_CTX mdctx;
 	void *mdblock;
@@ -104,12 +103,11 @@ static void gz_close(void *stream)
 	if (gis->cb != NULL)
 		EVP_MD_CTX_cleanup(&gis->mdctx);
 	inflateEnd(&gis->zs);
-	if (gis->autoclose)
-		gis->bs->close(gis->bs, NULL);
+	gis->bs->close(gis->bs, NULL);
 	free(gis);
 }
 
-struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *bs, int autoclose,
+struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *bs,
 					     apk_multipart_cb cb, void *ctx)
 {
 	struct apk_gzip_istream *gis;
@@ -126,7 +124,6 @@ struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *bs, int autoclo
 		.is.close = gz_close,
 		.bs = bs,
 		.z_err = 0,
-		.autoclose = autoclose,
 		.cb = cb,
 		.cbctx = ctx,
 	};
diff --git a/src/index.c b/src/index.c
index 62460f7bfbec72c90eab102edf15a305b4b2bfcc..d7402f9ee5dd74163d0df686b719d93c831776ee 100644
--- a/src/index.c
+++ b/src/index.c
@@ -48,7 +48,7 @@ static int index_read_file(struct apk_database *db, struct index_ctx *ictx)
 	if (apk_file_get_info(ictx->index, APK_CHECKSUM_NONE, &fi) < 0)
 		return -1;
 	ictx->index_mtime = fi.mtime;
-	bs = apk_bstream_from_istream(apk_bstream_gunzip(apk_bstream_from_url(ictx->index), 1));
+	bs = apk_bstream_from_istream(apk_bstream_gunzip(apk_bstream_from_url(ictx->index)));
 	if (bs == NULL)
 		return -1;
 	r = apk_db_index_read(db, bs, 0);
diff --git a/src/io.c b/src/io.c
index 335b14d3dbfb37ca4f11d734d1adc7eda594111b..26d51b986f82dd5122f6646fc7d0db8430c9ad5c 100644
--- a/src/io.c
+++ b/src/io.c
@@ -479,7 +479,7 @@ int apk_file_get_info(const char *filename, int checksum, struct apk_file_info *
 
 struct apk_istream *apk_istream_from_file_gz(const char *file)
 {
-	return apk_bstream_gunzip(apk_bstream_from_file(file), TRUE);
+	return apk_bstream_gunzip(apk_bstream_from_file(file));
 }
 
 struct apk_fd_ostream {
diff --git a/src/package.c b/src/package.c
index 86595c916c07757fa045567fe294605791c0d5b5..c01a5e8baafa77c2c99ad3fcbedf8689c12483f9 100644
--- a/src/package.c
+++ b/src/package.c
@@ -430,12 +430,15 @@ static int apk_pkg_gzip_part(void *ctx, EVP_MD_CTX *mdctx, int part)
 struct apk_package *apk_pkg_read(struct apk_database *db, const char *file)
 {
 	struct read_info_ctx ctx;
+	struct apk_file_info fi;
 	struct apk_bstream *bs;
 	struct apk_istream *tar;
 	char realfile[PATH_MAX];
 
 	if (realpath(file, realfile) < 0)
 		return NULL;
+	if (apk_file_get_info(realfile, APK_CHECKSUM_NONE, &fi) < 0)
+		return NULL;
 
 	ctx.pkg = apk_pkg_new();
 	if (ctx.pkg == NULL)
@@ -447,14 +450,15 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file)
 
 	ctx.db = db;
 	ctx.has_install = 0;
+	ctx.pkg->size = fi.size;
 
-	tar = apk_bstream_gunzip_mpart(bs, FALSE, apk_pkg_gzip_part, &ctx);
+	tar = apk_bstream_gunzip_mpart(bs, apk_pkg_gzip_part, &ctx);
 	if (apk_parse_tar(tar, read_info_entry, &ctx) < 0) {
 		apk_error("File %s is not an APK archive", file);
 		bs->close(bs, NULL);
 		goto err;
 	}
-	bs->close(bs, &ctx.pkg->size);
+	tar->close(tar);
 
 	if (ctx.pkg->name == NULL) {
 		apk_error("File %s is corrupted", file);
diff --git a/src/url.c b/src/url.c
index c010ea8d60a92211850482a0ed6628741a2df505..e09ec4c8c94274734b7141ec43290ae9c37ecba3 100644
--- a/src/url.c
+++ b/src/url.c
@@ -4,7 +4,7 @@
  * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
  * All rights reserved.
  *
- * This program is free software; you can redistribute it and/or modify it 
+ * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 as published
  * by the Free Software Foundation. See http://www.gnu.org/ for details.
  */
@@ -68,7 +68,7 @@ struct apk_istream *apk_istream_from_url(const char *url)
 
 struct apk_istream *apk_istream_from_url_gz(const char *file)
 {
-	return apk_bstream_gunzip(apk_bstream_from_url(file), TRUE);
+	return apk_bstream_gunzip(apk_bstream_from_url(file));
 }
 
 struct apk_bstream *apk_bstream_from_url(const char *url)