This is a migrated thread and some comments may be shown as answers.

Corrupt Zip Archive; Last file always has some error with it

2 Answers 388 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 11 May 2020, 06:44 PM

I'm using Telerik in an interop environment with C++ and trying to replace our old ZIP library.  With the code below it appears to ZIP all files correctly except the last file always has some error with it like it isn't being written out properly.  I've changed the number and type of files and the specific file type doesn't matter, the last one in the archive is always corrupted when i test the archive.

 

    System::String^ sSource = gcnew System::String(m_sPath1);
        System::String^ sDest = gcnew System::String(sZipDest);
        
        System::IO::File^ file = nullptr;
        System::IO::Stream^ stream = file->Open(sDest, System::IO::FileMode::Create);
        Telerik::Windows::Zip::ZipArchive^ zarchive = gcnew Telerik::Windows::Zip::ZipArchive(stream, Telerik::Windows::Zip::ZipArchiveMode::Create,false,System::Text::Encoding::UTF8);
         
        Telerik::Windows::Zip::DeflateSettings^ compressionSettings = gcnew Telerik::Windows::Zip::DeflateSettings();
        compressionSettings->CompressionLevel = Telerik::Windows::Zip::CompressionLevel::Best;
        compressionSettings->HeaderType = Telerik::Windows::Zip::CompressedStreamHeader::ZLib;
 
        pList3->ResetContent();
        rc = 0;
        nCount = pList1->GetCount();
        for (n = 0; n<nCount; n++) {
            pList1->GetText(n, sFileName);
            sFullyQualifiedFileNameFrom = fs.AppendWildcard(m_sPath1, sFileName);
            int nSel = pList3->AddString(sFileName);
            pList3->SetCurSel(nSel);
            pList3->UpdateWindow();
            System::String^ sFull = gcnew System::String(sFullyQualifiedFileNameFrom.GetBuffer());
            System::String^ sName = gcnew System::String(sFileName.GetBuffer());
            Telerik::Windows::Zip::Extensions::ZipFile::CreateEntryFromFile(zarchive, sFull, sName, compressionSettings);
            int nFilesPercent = 100 * (n + 1) / nCount;
            m_prStatus.SetPos(nFilesPercent);
        }
        Sleep(5000);
        stream->Flush();
        stream->Close();

 

Is there something else that should be done to finish the compression/writing of the last file when in the loop using CreateEntryFromFile?  

2 Answers, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 12 May 2020, 03:08 PM

In case it helps others, to avoid this issue in this interop environment you must manually delete the ZipArchive object in order to successfully flush/close the stream out.

simply added "delete zarchive" after the loop and now the archive works perfectly and removed the lines flushing/closing the stream itself.

0
Martin
Telerik team
answered on 13 May 2020, 07:10 AM

Hello Eric,

I have already answered you on this topic in the support ticket you had opened and I will copy the answer here as well.

This behavior seems to be related to not released unmanaged resources. I would suggest you to dispose of the ZipArchive ("zarchive") after the loop and before you call the Flush method of the stream.

As a side note, the Flush method, in this case, is not necessary because the stream closes when the ZipArchive is disposed of when you set the leaveOpen parameter of the ZipArchive constructor to false (leaveOpen: True to leave the stream open after the ZipArchive object is disposed; otherwise, false.): 

Telerik::Windows::Zip::ZipArchive^ zarchive = gcnew Telerik::Windows::Zip::ZipArchive(stream, Telerik::Windows::Zip::ZipArchiveMode::Create,false,System::Text::Encoding::UTF8);

Regards,
Martin
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
ZipLibrary
Asked by
Eric
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Martin
Telerik team
Share this question
or