Esempio n. 1
0
int
main(int argc, char *argv[])
{
    int fail, ze;
    struct zip *z;
    struct zip_source *zs;
    char *archive;
    char errstr[1024];

    fail = 0;

    prg = argv[0];

    if (argc != 2) {
        fprintf(stderr, "usage: %s archive\n", prg);
        return 1;
    }

    archive = argv[1];

    if ((z=zip_open(archive, 0, &ze)) == NULL) {
	zip_error_to_str(errstr, sizeof(errstr), ze, errno);
	printf("%s: opening zip archive ``%s'' failed: %s\n",
	       prg, archive, errstr);
	return 1;
    }

    fail += do_read(z, "storedok", 0, WHEN_NEVER, 0, 0);
    fail += do_read(z, "deflateok", 0, WHEN_NEVER, 0, 0);
    fail += do_read(z, "storedcrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
    fail += do_read(z, "deflatecrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0);
    fail += do_read(z, "deflatezliberror", 0, WHEN_READ, ZIP_ER_ZLIB, -3);
    fail += do_read(z, NULL, 0, WHEN_OPEN, ZIP_ER_INVAL, 0);
    fail += do_read(z, "nosuchfile", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
    fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0,0);
    fail += do_read(z, "deflatecrcerror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);
    fail += do_read(z, "storedcrcerror", ZIP_FL_COMPRESSED,
		    WHEN_READ, ZIP_ER_CRC, 0);
    fail += do_read(z, "storedok", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0);

    zs = zip_source_buffer(z, "asdf", 4, 0);
    zip_replace(z, zip_name_locate(z, "storedok", 0), zs);
    fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
    fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
    zip_delete(z, zip_name_locate(z, "storedok", 0));
    fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0);
    fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0);
    zs = zip_source_buffer(z, "asdf", 4, 0);
    zip_add(z, "new_file", zs);
    fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0);
    zip_unchange_all(z);

    if (zip_close(z) == -1) {
        fprintf(stderr, "%s: can't close zip archive `%s'\n", prg, archive);
        return 1;
    }

    exit(fail ? 1 : 0);
}
Esempio n. 2
0
    void * GetFileData(const utf8 * path, size_t * outSize) const override
    {
        void * data = nullptr;

        size_t index = (size_t)zip_name_locate(_zip, path, 0);
        uint64 dataSize = GetFileSize(index);
        if (dataSize > 0 && dataSize < SIZE_MAX)
        {
            zip_file_t * zipFile = zip_fopen(_zip, path, 0);
            if (zipFile != nullptr)
            {
                data = Memory::Allocate<void>((size_t)dataSize);
                uint64 readBytes = zip_fread(zipFile, data, dataSize);
                if (readBytes != dataSize)
                {
                    Memory::Free(data);
                    data = nullptr;
                    dataSize = 0;
                }
                zip_fclose(zipFile);
            }
        }

        if (outSize != nullptr) *outSize = (size_t)dataSize;
        return data;
    }
Esempio n. 3
0
static VALUE zipruby_archive_add_or_replace_io(int argc, VALUE *argv, VALUE self) {
  VALUE name, io, flags;
  struct zipruby_archive *p_archive;
  int index, i_flags = 0;

  rb_scan_args(argc, argv, "21", &name, &io, &flags);
  Check_IO(io);

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  Check_Type(name, T_STRING);
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
  Check_Archive(p_archive);

  index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);

  if (index >= 0) {
    VALUE _args[] = {INT2NUM(index), io, flags};
    return zipruby_archive_replace_io(2, _args, self);
  } else {
    VALUE _args[2] = { name, io };
    return zipruby_archive_add_io(2, _args, self);
  }
}
Esempio n. 4
0
QByteArray zipobject::readData(const QString& inZipPath)const{
    if( NULL == m_zip){
        qDebug()<<"zip unopened";
        return QByteArray();
    }
    int index = zip_name_locate(m_zip,getCharPtr(inZipPath),0);
    if( -1 == index ){
        qDebug()<<"no such file : "<<inZipPath;
        return QByteArray();
    }
    zip_file *pFile = zip_fopen_index(m_zip,index,0);
    if( NULL == pFile){
        qDebug()<<"fopen is NULL"<<zip_strerror(m_zip);
        return QByteArray();
    }
    struct zip_stat stat;
    int rStat = zip_stat_index(m_zip,index, 0, &stat);
    if( -1 == rStat ){
        qDebug()<<"stat failed : "<<zip_strerror(m_zip);
        return QByteArray();
    }
    const int length = stat.size;
    char buffer[length +1 ];
    int rRead = zip_fread(pFile,buffer,sizeof(buffer));
    if( -1 == rRead ){
        qDebug()<<"read failed : "<<zip_strerror(m_zip);
        return QByteArray();
    }
    return QByteArray (buffer,rRead);
}
Esempio n. 5
0
bool zipobject::writeData(const QByteArray& data,const QString& inZipPath){
    TEST_ZIP;
    int length=data.length();
    void* p=new char[length+1];
    // 此处不得不复制。
    // 由于 libzip 只会在 close 的时候才会真正将数据写入zip文件
    // 如果不复制,写入数据时 data 已经析构,数据内容全部为空
    memcpy(p,data.constData(),length);
    zip_source* pSource = zip_source_buffer(m_zip,p,length,1);
    if(NULL == pSource){
        const char* pStr = zip_strerror(m_zip);
        qDebug()<<"create source failed : "<<pStr;
        return false;
    }else{
        int index = zip_name_locate(m_zip,getCharPtr(inZipPath),0);
        if( -1 == index ){
            int rAdd = zip_add(m_zip,getCharPtr(inZipPath),pSource);
            if( -1 == rAdd ){
                qDebug()<<"add source failed : "<<zip_strerror(m_zip);
            }
            return -1 == rAdd ;
        }else{
            int rReplace = zip_replace(m_zip,index,pSource);
            if( -1 == rReplace ){
                qDebug()<<"replace source failed : "<<zip_strerror(m_zip);
            }
            return -1 == rReplace ;
        }
    }
}
Esempio n. 6
0
int ipsw_get_file_size(const char* ipsw, const char* infile, off_t* size) {
	ipsw_archive* archive = ipsw_open(ipsw);
	if (archive == NULL || archive->zip == NULL) {
		error("ERROR: Invalid archive\n");
		return -1;
	}

	int zindex = zip_name_locate(archive->zip, infile, 0);
	if (zindex < 0) {
		error("ERROR: zip_name_locate: %s\n", infile);
		return -1;
	}

	struct zip_stat zstat;
	zip_stat_init(&zstat);
	if (zip_stat_index(archive->zip, zindex, 0, &zstat) != 0) {
		error("ERROR: zip_stat_index: %s\n", infile);
		return -1;
	}

	*size = zstat.size;

	ipsw_close(archive);
	return 0;
}
Esempio n. 7
0
static VALUE zipruby_archive_add_or_replace_function(int argc, VALUE *argv, VALUE self) {
  VALUE name, mtime, flags;
  struct zipruby_archive *p_archive;
  int index, i_flags = 0;

  rb_scan_args(argc, argv, "12", &name, &mtime, &flags);

  if (NIL_P(flags) && FIXNUM_P(mtime)) {
    flags = mtime;
    mtime = Qnil;
  }

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  Check_Type(name, T_STRING);
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
  Check_Archive(p_archive);

  index = zip_name_locate(p_archive->archive, RSTRING_PTR(name), i_flags);

  if (index >= 0) {
    VALUE _args[] = { INT2NUM(index), mtime };
    return zipruby_archive_replace_function(2, _args, self);
  } else {
    VALUE _args[] = { name, mtime };
    return zipruby_archive_add_function(2, _args, self);
  }
}
Esempio n. 8
0
int r_io_zip_flush_file(RIOZipFileObj *zfo) {
	int res = false;
	struct zip * zipArch;

	if (!zfo) {
		return res;
	}

	zipArch = r_io_zip_open_archive (
		zfo->archivename, zfo->perm, zfo->mode, zfo->rw);
	if (!zipArch) {
		return res;
	}

	struct zip_source *s = zip_source_buffer (zipArch, zfo->b->buf, zfo->b->length, 0);
	if (s && zfo->entry != -1) {
		if (zip_replace(zipArch, zfo->entry, s) == 0) {
			res = true;
		}
	} else if (s && zfo->name) {
		if (zip_add (zipArch, zfo->name, s) == 0) {
			zfo->entry = zip_name_locate (zipArch, zfo->name, 0);
			res = true;
		}
	}
	// s (zip_source) is freed when the archive is closed, i think - dso
	zip_close (zipArch);
	if (s) {
		zip_source_free (s);
	}
	return res;
}
Esempio n. 9
0
int r_io_zip_flush_file(RIOZipFileObj *zip_file_obj) {
	int result = R_FALSE;
	struct zip * zipArch = r_io_zip_open_archive(zip_file_obj->archivename, zip_file_obj->flags, zip_file_obj->mode, zip_file_obj->rw);  

	if (!zipArch) {
		return result;
	}

	if (zip_file_obj) {
		struct zip_source *s = zip_source_buffer(zipArch, zip_file_obj->b, zip_file_obj->b->length, 0);
		if (s && zip_file_obj->entry != -1) {

			if (zip_replace(zipArch, zip_file_obj->entry, s) == 0)
				result = R_TRUE;

		}else if (s && zip_file_obj->name) {

			if (zip_add(zipArch, zip_file_obj->name, s) == 0) {

				zip_file_obj->entry = zip_name_locate(zipArch, zip_file_obj->name, 0);
				result = R_TRUE;
			}
		}
		if (s)
			zip_source_free(s);		

	}

	if (zipArch)
		zip_close(zipArch);

	return result;
}
Esempio n. 10
0
ZIP_EXTERN zip_file_t *
zip_fopen_encrypted(zip_t *za, const char *fname, zip_flags_t flags, const char *password) {
    zip_int64_t idx;

    if ((idx = zip_name_locate(za, fname, flags)) < 0)
	return NULL;

    return zip_fopen_index_encrypted(za, (zip_uint64_t)idx, flags, password);
}
Esempio n. 11
0
static VALUE zipruby_archive_replace_function(int argc, VALUE *argv, VALUE self) {
  VALUE index, flags, mtime;
  struct zipruby_archive *p_archive;
  struct zip_source *zsource;
  struct read_proc *z;
  int i_index, i_flags = 0;

  rb_scan_args(argc, argv, "12", &index, &mtime, &flags);
  rb_need_block();

  if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
  }

  if (NIL_P(mtime)) {
    mtime = rb_funcall(rb_cTime, rb_intern("now"), 0);
  } else if (!rb_obj_is_instance_of(mtime, rb_cTime)) {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(mtime)));
  }

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  Data_Get_Struct(self, struct zipruby_archive, p_archive);
  Check_Archive(p_archive);

  if (FIXNUM_P(index)) {
    i_index = NUM2INT(index);
  } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
    rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
  }

  if ((z = malloc(sizeof(struct read_proc))) == NULL) {
    zip_unchange_all(p_archive->archive);
    zip_unchange_archive(p_archive->archive);
    rb_raise(rb_eRuntimeError, "Replace failed at %d: Cannot allocate memory", i_index);
  }

  z->proc = rb_block_proc();
  rb_ary_push(p_archive->sources, z->proc);
  z->mtime = TIME2LONG(mtime);

  if ((zsource = zip_source_proc(p_archive->archive, z)) == NULL) {
    free(z);
    rb_raise(Error, "Replace failed at %d: %s", i_index, zip_strerror(p_archive->archive));
  }

  if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
    zip_source_free(zsource);
    zip_unchange_all(p_archive->archive);
    zip_unchange_archive(p_archive->archive);
    rb_raise(Error, "Replace failed at %d: %s", i_index, zip_strerror(p_archive->archive));
  }

  return Qnil;
}
Esempio n. 12
0
ZIP_EXTERN int
zip_stat(struct zip *za, const char *fname, int flags, struct zip_stat *st)
{
    int idx;

    if ((idx=zip_name_locate(za, fname, flags)) < 0)
	return -1;

    return zip_stat_index(za, idx, flags, st);
}
Esempio n. 13
0
ZIP_EXTERN int
zip_stat(struct zip *za, const char *fname, zip_flags_t flags, struct zip_stat *st)
{
    zip_int64_t idx;

    if ((idx=zip_name_locate(za, fname, flags)) < 0)
	return -1;

    return zip_stat_index(za, (zip_uint64_t)idx, flags, st);
}
Esempio n. 14
0
ZIP_EXTERN struct zip_file *
zip_fopen(struct zip *za, const char *fname, int flags)
{
    int idx;

    if ((idx=zip_name_locate(za, fname, flags)) < 0)
	return NULL;

    return zip_fopen_index(za, idx, flags);
}
Esempio n. 15
0
int g_fileExists(const char* sz_file_name)
{
	if (g_p_zip_file != NULL)
	{
		int result = zip_name_locate(g_p_zip_file, sz_file_name, 0);
		return (result != -1) ? 1 : 0;
	}

	return 0;
}
Esempio n. 16
0
static STRBUF *read_from_zip(const char *zipfile, const char *filename)
{
	int r = 0;
	STRBUF *content = NULL;

#ifdef HAVE_LIBZIP
	int zip_error;
	struct zip *zip = NULL;
	struct zip_stat stat;
	struct zip_file *unzipped = NULL;
	char *buf = NULL;

	if ( !(zip = zip_open(zipfile, 0, &zip_error)) ||
	     (r = zip_name_locate(zip, filename, 0)) < 0 ||
	     (zip_stat_index(zip, r, ZIP_FL_UNCHANGED, &stat) < 0) ||
	     !(unzipped = zip_fopen_index(zip, r, ZIP_FL_UNCHANGED)) ) {
		if (unzipped)
			zip_fclose(unzipped);
		if (zip)
			zip_close(zip);
		r = -1;
	}
#else
	r = kunzip_get_offset_by_name((char*)zipfile, (char*)filename, 3, -1);
#endif

	if(-1 == r) {
		fprintf(stderr,
			"Can't read from %s: Is it an OpenDocument Text?\n", zipfile);
		exit(EXIT_FAILURE);
	}

#ifdef HAVE_LIBZIP
	if ( !(buf = ymalloc(stat.size + 1)) ||
	     (zip_fread(unzipped, buf, stat.size) != stat.size) ||
	     !(content = strbuf_slurp_n(buf, stat.size)) ) {
		if (buf)
			yfree(buf);
		content = NULL;
	}
	zip_fclose(unzipped);
	zip_close(zip);
#else
	content = kunzip_next_tobuf((char*)zipfile, r);
#endif

	if (!content) {
		fprintf(stderr,
			"Can't extract %s from %s.  Maybe the file is corrupted?\n",
			filename, zipfile);
		exit(EXIT_FAILURE);
	}

	return content;
}
static int zip_get_app_directory(struct zip* zf, char** path)
{
	int i = 0;
	int c = zip_get_num_files(zf);
	int len = 0;
	const char* name = NULL;

	/* look through all filenames in the archive */
	do {
		/* get filename at current index */
		name = zip_get_name(zf, i++, 0);
		if (name != NULL) {
			/* check if we have a "Payload/.../" name */
			len = strlen(name);
			if (!strncmp(name, "Payload/", 8) && (len > 8)) {
				/* locate the second directory delimiter */
				const char* p = name + 8;
				do {
					if (*p == '/') {
						break;
					}
				} while(p++ != NULL);

				/* try next entry if not found */
				if (p == NULL)
					continue;

				len = p - name + 1;

				if (*path != NULL) {
					free(*path);
					*path = NULL;
				}

				/* allocate and copy filename */
				*path = (char*)malloc(len + 1);
				strncpy(*path, name, len);

				/* add terminating null character */
				char* t = *path + len;
				*t = '\0';
				break;
			}
		}
	} while(i < c);

	/* check if the path actually exists */
	int zindex = zip_name_locate(zf, *path, 0);
	if (zindex < 0) {
		return -1;
	}

	return 0;
}
int
find_success(struct zip *z, const char *name, int flags)
{

    if (zip_name_locate(z, name, flags) < 0) {
	printf("%s: unexpected error while looking for ``%s'': %s\n",
	       prg, name, zip_strerror(z));
	return 1;
    }

    return 0;
}
Esempio n. 19
0
AbstractFSProvider::status_t ZIPProvider::ZIPHandle::removeDir(const FileName & directory) {
	int index = zip_name_locate(handle, directory.getPath().c_str(), 0);
	if (index == -1) {
		WARN(zip_strerror(handle));
		return FAILURE;
	}
	if (zip_delete(handle, index) == -1) {
		WARN(zip_strerror(handle));
		return FAILURE;
	}
	dataWritten = true;
	return OK;
}
Esempio n. 20
0
 void SetFileData(const utf8 * path, void * data, size_t dataSize) override
 {
     zip_source_t * source = zip_source_buffer(_zip, data, dataSize, 0);
     sint64 index = zip_name_locate(_zip, path, 0);
     if (index == -1)
     {
         zip_add(_zip, path, source);
     }
     else
     {
         zip_replace(_zip, index, source);
     }
 }
Esempio n. 21
0
static VALUE zipruby_archive_replace_buffer(int argc, VALUE *argv, VALUE self) {
  struct zipruby_archive *p_archive;
  struct zip_source *zsource;
  VALUE index, source, flags;
  int i_index, i_flags = 0;
  char *data;
  size_t len;

  rb_scan_args(argc, argv, "21", &index, &source, &flags);

  if (TYPE(index) != T_STRING && !FIXNUM_P(index)) {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or String)", rb_class2name(CLASS_OF(index)));
  }

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  Check_Type(source, T_STRING);
  Data_Get_Struct(self, struct zipruby_archive, p_archive);
  Check_Archive(p_archive);

  if (FIXNUM_P(index)) {
    i_index = NUM2INT(index);
  } else if ((i_index = zip_name_locate(p_archive->archive, RSTRING_PTR(index), i_flags)) == -1) {
    rb_raise(Error, "Replace file failed - %s: Archive does not contain a file", RSTRING_PTR(index));
  }

  len = RSTRING_LEN(source);

  if ((data = malloc(len)) == NULL) {
    rb_raise(rb_eRuntimeError, "Replace file failed: Cannot allocate memory");
  }

  memcpy(data, RSTRING_PTR(source), len);

  if ((zsource = zip_source_buffer(p_archive->archive, data, len, 1)) == NULL) {
    free(data);
    rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
  }

  if (zip_replace(p_archive->archive, i_index, zsource) == -1) {
    zip_source_free(zsource);
    zip_unchange_all(p_archive->archive);
    zip_unchange_archive(p_archive->archive);
    rb_raise(Error, "Replace file failed at %d: %s", i_index, zip_strerror(p_archive->archive));
  }

  return Qnil;
}
Esempio n. 22
0
int ipsw_extract_to_memory(const char* ipsw, const char* infile, unsigned char** pbuffer, unsigned int* psize) {
	ipsw_archive* archive = ipsw_open(ipsw);
	if (archive == NULL || archive->zip == NULL) {
		error("ERROR: Invalid archive\n");
		return -1;
	}

	int zindex = zip_name_locate(archive->zip, infile, 0);
	if (zindex < 0) {
		error("ERROR: zip_name_locate: %s\n", infile);
		return -1;
	}

	struct zip_stat zstat;
	zip_stat_init(&zstat);
	if (zip_stat_index(archive->zip, zindex, 0, &zstat) != 0) {
		error("ERROR: zip_stat_index: %s\n", infile);
		return -1;
	}

	struct zip_file* zfile = zip_fopen_index(archive->zip, zindex, 0);
	if (zfile == NULL) {
		error("ERROR: zip_fopen_index: %s\n", infile);
		return -1;
	}

	int size = zstat.size;
	unsigned char* buffer = (unsigned char*) malloc(size+1);
	if (buffer == NULL) {
		error("ERROR: Out of memory\n");
		zip_fclose(zfile);
		return -1;
	}

	if (zip_fread(zfile, buffer, size) != size) {
		error("ERROR: zip_fread: %s\n", infile);
		zip_fclose(zfile);
		free(buffer);
		return -1;
	}

	buffer[size] = '\0';

	zip_fclose(zfile);
	ipsw_close(archive);

	*pbuffer = buffer;
	*psize = size;
	return 0;
}
Esempio n. 23
0
cnpy::NpArray cnpy::npz_load(const std::string& fname, const std::string& varname)
{
    Handler<struct zip> zip = zip_open(fname.c_str(), ZIP_CHECKCONS, nullptr);
    if(zip.handle()==nullptr)
        throw std::runtime_error("Error opening npz file "+fname);

    std::string key = varname + ".npy";
    int nameLookup = zip_name_locate(zip.handle(), key.c_str(), 0);
    if(nameLookup<0)
        throw std::runtime_error("Variable name "+varname+" not found in "+fname);

    Handler<struct zip_file> zipFile = zip_fopen_index(zip.handle(), nameLookup, 0);

    NpArray array = load_the_npy_file(zipFile);
    return array;
}
Esempio n. 24
0
static VFSNode* vfs_zipfile_locate(VFSNode *node, const char *path) {
	VFSZipFileTLS *tls = vfs_zipfile_get_tls(node, true);
	VFSZipFileData *zdata = node->data1;
	int64_t idx;

	if(!ht_lookup(&zdata->pathmap, path, &idx)) {
		idx = zip_name_locate(tls->zip, path, 0);
	}

	if(idx < 0) {
		return NULL;
	}

	VFSNode *n = vfs_alloc();
	vfs_zippath_init(n, node, idx);
	return n;
}
Esempio n. 25
0
static VALUE zipruby_archive_locate_name(int argc, VALUE *argv, VALUE self) {
  VALUE fname, flags;
  struct zipruby_archive *p_archive;
  int i_flags = 0;

  rb_scan_args(argc, argv, "11", &fname, &flags);
  Check_Type(fname, T_STRING);

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  Data_Get_Struct(self, struct zipruby_archive, p_archive);
  Check_Archive(p_archive);

  return INT2NUM(zip_name_locate(p_archive->archive, RSTRING_PTR(fname), i_flags));
}
Esempio n. 26
0
int ipsw_file_exists(const char* ipsw, const char* infile)
{
	ipsw_archive* archive = ipsw_open(ipsw);
	if (archive == NULL || archive->zip == NULL) {
		return -1;
	}

	int zindex = zip_name_locate(archive->zip, infile, 0);
	if (zindex < 0) {
		ipsw_close(archive);
		return -2;
	}

	ipsw_close(archive);

	return 0;
}
Esempio n. 27
0
AbstractFSProvider::status_t ZIPProvider::ZIPHandle::writeFile(const FileName & file, const std::vector<uint8_t> & data, bool overwrite) {
	if (file.getFile().empty()) {
		return FAILURE;
	}

	bool replace = false;

	int index = zip_name_locate(handle, file.getPath().c_str(), 0);
	if (index != -1) {
		// File already exists.
		if (!overwrite) {
			return FAILURE;
		} else {
			replace = true;
		}
	}

	// Store data temporary because libzip writes data not until zip_close.
	tempStore.emplace_back(data);

	zip_source * source = zip_source_buffer(handle,
			tempStore.back().data(), static_cast<off_t>(tempStore.back().size()), 0);
	if (source == nullptr) {
		WARN(zip_strerror(handle));
		zip_source_free(source);
		tempStore.pop_back();
		return FAILURE;
	}

	int newIndex;
	if (replace) {
		newIndex = zip_replace(handle, index, source);
	} else {
		newIndex = zip_add(handle, file.getPath().c_str(), source);
	}
	if (newIndex == -1) {
		WARN(zip_strerror(handle));
		zip_source_free(source);
		tempStore.pop_back();
		return FAILURE;
	}
	dataWritten = true;
	return OK;
}
static int zip_get_contents(struct zip *zf, const char *filename, int locate_flags, char **buffer, uint32_t *len)
{
	struct zip_stat zs;
	struct zip_file *zfile;
	int zindex = zip_name_locate(zf, filename, locate_flags);

	*buffer = NULL;
	*len = 0;

	if (zindex < 0) {
		return -1;
	}

	zip_stat_init(&zs);

	if (zip_stat_index(zf, zindex, 0, &zs) != 0) {
		fprintf(stderr, "ERROR: zip_stat_index '%s' failed!\n", filename);
		return -2;
	}

	if (zs.size > 10485760) {
		fprintf(stderr, "ERROR: file '%s' is too large!\n", filename);
		return -3;
	}

	zfile = zip_fopen_index(zf, zindex, 0);
	if (!zfile) {
		fprintf(stderr, "ERROR: zip_fopen '%s' failed!\n", filename);
		return -4;
	}

	*buffer = malloc(zs.size);
	if (zs.size > LLONG_MAX || zip_fread(zfile, *buffer, zs.size) != (zip_int64_t)zs.size) {
		fprintf(stderr, "ERROR: zip_fread %" PRIu64 " bytes from '%s'\n", (uint64_t)zs.size, filename);
		free(*buffer);
		*buffer = NULL;
		zip_fclose(zfile);
		return -5;
	}
	*len = zs.size;
	zip_fclose(zfile);
	return 0;
}
Esempio n. 29
0
void cnpy::npz_save_data(const std::string& zipname, const std::string& name,
                         const unsigned char* data, const cnpy::Type dtype,
                         const size_t elemSize, const std::vector<size_t>& shape,
                         const char mode)
{
    //first, append a .npy to the fname
    std::string fname(name);
    fname += ".npy";

    if(mode=='w' && std::ifstream(zipname).is_open())
    {
        // Remove the old file if present
        if(std::remove(zipname.c_str())!=0)
            throw std::runtime_error("Unable to overwrite "+zipname);
    }

    Handler<struct zip> zip = zip_open(zipname.c_str(), ZIP_CREATE, nullptr);
    if(zip.handle()==nullptr)
        throw std::runtime_error("Error opening npz file "+zipname);

    // Remove the old array if present
    int nameLookup = zip_name_locate(zip.handle(), fname.c_str(), 0);
    if(nameLookup>=0 && zip_delete(zip.handle(), nameLookup)!=0)
        throw std::runtime_error("Unable to overwrite "+name+" array");

    std::vector<char> header = create_npy_header(dtype, elemSize, shape);

    const int dataSize = std::accumulate(shape.cbegin(), shape.cend(), elemSize, std::multiplies<size_t>());
    ZipSourceCallbackData cbData(header, data, dataSize);

    Handler<struct zip_source> zipSource = zip_source_function(zip.handle(), zipSourceCallback, &cbData);
    if(zipSource.handle()==nullptr)
        throw std::runtime_error("Error creating "+name+" array");

    zip_int64_t fid = zip_add(zip.handle(), fname.c_str(), zipSource.handle());
    if(fid<0)
    {
        zip_source_free(zipSource.handle());
        throw std::runtime_error("Error creating "+name+" array");
    }

    zip.close();
}
Esempio n. 30
0
static int S_archive_name_locate(lua_State* L) {
    struct zip** ar    = check_archive(L, 1);
    const char*  fname = luaL_checkstring(L, 2);
    int          flags = (lua_gettop(L) < 3) ? 0 : luaL_checkint(L, 3);
    int          idx;

    if ( ! *ar ) return 0;

    idx = zip_name_locate(*ar, fname, flags);

    if ( idx < 0 ) {
        lua_pushnil(L);
        lua_pushstring(L, zip_strerror(*ar));
        return 2;
    }

    lua_pushinteger(L, idx+1);
    return 1;
}