Add multiple file in zip format to memory Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
zhouhao
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2005 7:02 am

Add multiple file in zip format to memory

Post by zhouhao »

I know I can add multiple files with directory structure into a file by using wxZipOutputStream. But my question is can I add files to memory? I tried the following code. It seems that it doesn't work.

Code: Select all


    wxMemoryOutputStream mem;
    wxZipOutputStream* zip = new wxZipOutputStream(mem);
    wxFFileInputStream 	in(_T("test.txt"));

    zip->PutNextEntry(_T("folder1/test.txt"));
    zip->Write(in);

    size_t size1 = mem.GetSize();
    size_t size2 = zip->GetSize();

    zip->Close();

The size1 and size2 both are 4. Obviously it's not try. What's wrong with it? Can anybody help?

Thanks
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
some time ago i had used the same code, it worked fine. One difference is that i got size of the memory stream by TellO() (i don't remember at the moment why i did that :)).

Code: Select all

bool wxDbVibroAv::Update(wxInputStream *stream)
{
    wxMemoryOutputStream mst(0, 0);
    wxZipOutputStream    zst(mst);
    zst.PutNextEntry("Data");
    zst.Write(*stream);
    zst.Close();
    m_d.size = mst.TellO();
...
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
zhouhao
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2005 7:02 am

Post by zhouhao »

tan wrote:Hi,
some time ago i had used the same code, it worked fine. One difference is that i got size of the memory stream by TellO() (i don't remember at the moment why i did that :)).

Code: Select all

bool wxDbVibroAv::Update(wxInputStream *stream)
{
    wxMemoryOutputStream mst(0, 0);
    wxZipOutputStream    zst(mst);
    zst.PutNextEntry("Data");
    zst.Write(*stream);
    zst.Close();
    m_d.size = mst.TellO();
...
Thanks for your reply. But Tell0 returns 4 also.

I tried to replace wxMemoryOutputStream to wxFFileOutputStream. The zip file was written successfully. But the size is also 4. So I think the data is already written to memory buffer already. If this is the case, my next question will be how can I get the buffer data from wxMemoryOutputStream?
zhouhao
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2005 7:02 am

Post by zhouhao »

I know how to do now. I have to put "size_t bufsize = mem3.GetSize();" after the wxZipOutputStream closed because wxZipOutputStream will do some more actual writing after Close(). Thanks for your help and reminding, tan. Actually it doesn't matter which method to take. The key point is to call them after Close().
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

i used CopyTo(char *buffer, size_t len) method:

Code: Select all

...
    m_d.size = mst.TellO();
    if( m_d.size > m_d.buf_size )
    {
        m_d.buf = (char *)realloc(m_d.buf, m_d.size);
        m_d.buf_size = m_d.size;
    }
    mst.CopyTo(m_d.buf, m_d.size);
...
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Post Reply