+enum compress_type {
+ ct_none, /* Unknown compression type */
+ ct_tar, /* Plain tar file */
+ ct_compress,
+ ct_gzip,
+ ct_bzip2,
+ ct_lzip,
+ ct_lzma,
+ ct_lzop,
+ ct_xz
+};
+
+static enum compress_type archive_compression_type = ct_none;
+
+struct zip_magic
+{
+ enum compress_type type;
+ size_t length;
+ char const *magic;
+};
+
+struct zip_program
+{
+ enum compress_type type;
+ char const *program;
+ char const *option;
+};
+
+static struct zip_magic const magic[] = {
+ { ct_none, 0, 0 },
+ { ct_tar, 0, 0 },
+ { ct_compress, 2, "\037\235" },
+ { ct_gzip, 2, "\037\213" },
+ { ct_bzip2, 3, "BZh" },
+ { ct_lzip, 4, "LZIP" },
+ { ct_lzma, 6, "\xFFLZMA" },
+ { ct_lzop, 4, "\211LZO" },
+ { ct_xz, 6, "\xFD" "7zXZ" },
+};
+
+#define NMAGIC (sizeof(magic)/sizeof(magic[0]))
+
+static struct zip_program zip_program[] = {
+ { ct_compress, COMPRESS_PROGRAM, "-Z" },
+ { ct_compress, GZIP_PROGRAM, "-z" },
+ { ct_gzip, GZIP_PROGRAM, "-z" },
+ { ct_bzip2, BZIP2_PROGRAM, "-j" },
+ { ct_bzip2, "lbzip2", "-j" },
+ { ct_lzip, LZIP_PROGRAM, "--lzip" },
+ { ct_lzma, LZMA_PROGRAM, "--lzma" },
+ { ct_lzma, XZ_PROGRAM, "-J" },
+ { ct_lzop, LZOP_PROGRAM, "--lzop" },
+ { ct_xz, XZ_PROGRAM, "-J" },
+ { ct_none }
+};
+
+static struct zip_program const *
+find_zip_program (enum compress_type type, int *pstate)
+{
+ int i;
+
+ for (i = *pstate; zip_program[i].type != ct_none; i++)