Exemplo n.º 1
0
struct zip_source *
zip_source_filep(struct zip *za, const char *file, off_t start, off_t len)
{
    struct read_file *f;
    struct zip_source *zs;

    if (za == NULL)
        return NULL;

    if (file == NULL || start < 0 || len < -1) {
        _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
        return NULL;
    }

    if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
        _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
        return NULL;
    }

    f->f = NULL;
    f->fname = strdup(file);
    f->off = start;
    f->len = (len ? len : -1);
    
    if ((zs=zip_source_function(za, read_file, f)) == NULL) {
        free(f);
        return NULL;
    }

    return zs;
}
Exemplo n.º 2
0
ZIP_EXTERN struct zip_source *
zip_source_buffer(struct zip *za, const void *data, zip_uint64_t len, int freep)
{
    struct read_data *f;
    struct zip_source *zs;

    if (za == NULL)
	return NULL;

    if (data == NULL && len > 0) {
	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
	return NULL;
    }

    if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) {
	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
	return NULL;
    }

    f->data = (const char *)data;
    f->end = ((const char *)data)+len;
    f->freep = freep;
    f->mtime = time(NULL);
    
    if ((zs=zip_source_function(za, read_data, f)) == NULL) {
	free(f);
	return NULL;
    }

    return zs;
}
Exemplo n.º 3
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();
}
Exemplo n.º 4
0
struct zip_source *
_zip_source_file_or_p(struct zip *za, const char *fname, FILE *file,
		      zip_uint64_t start, zip_int64_t len, int closep,
		      const struct zip_stat *st)
{
    struct read_file *f;
    struct zip_source *zs;

    if (file == NULL && fname == NULL) {
	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
	return NULL;
    }

    if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
	return NULL;
    }

    f->fname = NULL;
    if (fname) {
	if ((f->fname=strdup(fname)) == NULL) {
	    _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
	    free(f);
	    return NULL;
	}
    }
    f->f = file;
    f->off = start;
    f->len = (len ? len : -1);
    f->closep = f->fname ? 1 : closep;
    if (st)
	memcpy(&f->st, st, sizeof(f->st));
    else
	zip_stat_init(&f->st);

    if ((zs=zip_source_function(za, read_file, f)) == NULL) {
	free(f);
	return NULL;
    }

    return zs;
}
Exemplo n.º 5
0
struct zip_source *
zip_source_zip(struct zip *za, struct zip *srcza, int srcidx, int flags,
	       off_t start, off_t len)
{
    struct zip_error error;
    struct zip_source *zs;
    struct read_zip *p;

    if (za == NULL)
	return NULL;

    if (srcza == NULL || start < 0 || len < -1 || srcidx < 0 || srcidx >= srcza->nentry) {
	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
	return NULL;
    }

    if ((flags & ZIP_FL_UNCHANGED) == 0
	&& ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) {
	_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
	return NULL;
    }

    if (len == 0)
	len = -1;

    if (start == 0 && len == -1)
	flags |= ZIP_FL_COMPRESSED;
    else
	flags &= ~ZIP_FL_COMPRESSED;

    if ((p=(struct read_zip *)malloc(sizeof(*p))) == NULL) {
	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
	return NULL;
    }
	
    _zip_error_copy(&error, &srcza->error);
	
    if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0
	|| (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) {
	free(p);
	_zip_error_copy(&za->error, &srcza->error);
	_zip_error_copy(&srcza->error, &error);
	
	return NULL;
    }
    p->off = start;
    p->len = len;

    if ((flags & ZIP_FL_COMPRESSED) == 0) {
	p->st.size = p->st.comp_size = len;
	p->st.comp_method = ZIP_CM_STORE;
	p->st.crc = 0;
    }
    
    if ((zs=zip_source_function(za, read_zip, p)) == NULL) {
	free(p);
	return NULL;
    }

    return zs;
}
struct zip_source *zip_source_proc(struct zip *za, struct read_proc *z) {
  struct zip_source *zs;
  zs = zip_source_function(za, read_proc, z);
  return zs;
}