List Archive

Thread

Thread Index

Message

From: Qifei Wang <qfwang%google.com@localhost>
To: libzip-discuss%nih.at@localhost, libzip%nih.at@localhost
Subject: Failed to unzip file by zip_fdopen with the error code ZIP_ER_OPNOTSUPP
Date: Fri, 6 Sep 2019 18:03:06 -0700

Hi there,

  I am trying to unzip a zip file from file descriptor by the following code snippet. It reports the error ZIP_ER_OPNOTSUPP. The file descriptor is valid. The error seems reported by zip_open_from_source() method. See https://github.com/nih-at/libzip/blob/master/lib/zip_open.c#L106. Any idea about this?

  zip_t* archive = zip_fdopen(file_descriptor, O_RDONLY, &errorp);
  if (!archive) {
    // Error report here.
  }

  zip_int64_t num = zip_get_num_entries(archive, ZIP_FL_UNCHANGED);
  for (zip_uint64_t i = 0; i < num; ++i) {
    const char* name = zip_get_name(archive, i, ZIP_FL_ENC_GUESS);
    zip_file_t* zip_file = zip_fopen(archive, name, ZIP_FL_ENC_GUESS);
    zip_stat_t zip_file_stat;
    zip_stat_init(&zip_file_stat);
    zip_stat_index(archive, /*index=*/i, /*flags=*/0, &zip_file_stat);
    auto buf = absl::make_unique<char[]>(zip_file_stat.size);
    zip_int64_t size_read = zip_fread(zip_file, buf.get(), zip_file_stat.size);
    if (size_read == -1) {
      // Error handling.
    }
    const std::string output_filename = file::JoinPath(dir, name);
    // Extract to output_filename.
    int zip_file_close_result = zip_fclose(zip_file);
    if (zip_file_close_result != 0) {
      // Error handling.
    }
  }
  if (zip_close(archive) != 0) {
    // Error handling.
  }

Cheers,

Qifei Wang

Made by MHonArc.