kuba-- / zip

A portable, simple zip library written in C
MIT License
1.39k stars 272 forks source link

Issue when deleting files #327

Closed anass114 closed 8 months ago

anass114 commented 8 months ago

Hi,

I am getting an issue when I try to delete entries in my zip. I am using the following code :

                zip_t* zip = zip_open(it.first.c_str(), 0, 'r');
                for (int i = 0; i < zip_entries_total(zip); i++)
                {
                    if (zip_entry_openbyindex(zip, i) == 0)
                    {
                        std::string filename = zip_entry_name(zip);
                        if (it.second.count(filename) > 0)
                        {
                            indexes.push_back(zip_entry_index(zip));
                        }
                    }
                    zip_entry_close(zip);
                }
                zip_close(zip);
                zip = zip_open(it.first.c_str(), 0, 'd');
                zip_entries_deletebyindex(zip, &indexes[0], indexes.size());
                zip_close(zip);

By debugging, I see that my list if full of indexes. But after execution and zip closing. The size of the zip doesn't change and files that should be get deleted are not.

Am I doing something wrong ?

kuba-- commented 8 months ago

What is indexes? Is it a std::vector of size_t? If yes, have you tried passing indexes.data() instead of &indexes[0]?

anass114 commented 8 months ago

I forget to copy the line, but yes it's a size_t vector.

Seems with .data() it's working. Thanks for your help !

kuba-- commented 8 months ago

I forget to copy the line, but yes it's a size_t vector.

Seems with .data() it's working. Thanks for your help !

Yeah, you need to pass a pointer to size_t elements, no to the vector's container. Anyway, if data() is workable for then great.