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

How to extract a file

12 Answers 686 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Silvan
Top achievements
Rank 1
Silvan asked on 27 Oct 2011, 12:41 PM
Hi

I not figure out how to save a (large) binary file to disk from a zip-Package back to a file.

Thanks for help

12 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 01 Nov 2011, 08:51 AM
Hi Marcel,

 Could  you please check out this help article demonstrating how to get started with RadZipLibrary? Please let us know if you need further assistance. We would be glad to help you.

Best wishes,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Lee
Top achievements
Rank 1
answered on 17 Nov 2011, 03:10 PM
I'm having a similar issue with extracting files from a zip archive, and the documentation you reference is not helping.

Can you provide a sample project that actually extracts the files from the zip archive and writes them to disk?
0
Silvan
Top achievements
Rank 1
answered on 18 Nov 2011, 05:30 PM
Hi

After a lot of trying i create the following sample how to UnZip a file:

Private Sub UnZIP()
 
    'UnZip
    Using zipPackage As Telerik.Windows.Zip.ZipPackage = zipPackage.OpenFile("C:\temp\xyz.zip", IO.FileAccess.Read)
 
        If Not zipPackage.ZipPackageEntries.Count = 0 Then
            Dim zipEntry As ZipPackageEntry = zipPackage.ZipPackageEntries(0)
 
            'Create ZIP-stream
            Dim zipInp As ZipInputStream = New ZipInputStream(zipEntry.OpenInputStream)
 
            'Create file-stream
            Dim fileStream As New FileStream("c:\temp\xyz.txt", FileMode.Create, FileAccess.Write)
 
            'Write source-stream nach target-stream
            ReadWriteStream(zipInp.BaseStream, fileStream)
 
        End If
    End Using
 
End Sub
 
Private Sub ReadWriteStream(stmRead As Stream, stmWrite As Stream)
 
    Dim Length As Integer = 1024
    Dim buffer As [Byte]() = New [Byte](Length - 1) {}
    Dim bytesRead As Integer = stmRead.Read(buffer, 0, Length)
 
    'write the bytes
    While bytesRead > 0
        stmWrite.Write(buffer, 0, bytesRead)
        bytesRead = stmRead.Read(buffer, 0, Length)
    End While
 
    stmRead.Close()
    stmWrite.Close()
 
End Sub

My tip: if you like it easier, use the free DotNetZip Library which provide a lot (more) easy-to-use functions.
0
Lee
Top achievements
Rank 1
answered on 18 Nov 2011, 08:28 PM
Thanks.  Turns out I was closer than I thought.

Your help is greatly appreciated.

I'm actually already using the DotNetZip Library, but I thought I would try to use Telerik's zip library since I'm using their controls for everything else.  But you're right, DotNetZip is much easier.

Actually, Telerik's isn't that difficult, but the documentation is terribly incomplete.
0
Tina Stancheva
Telerik team
answered on 22 Nov 2011, 06:24 PM
Hi Lee,

We will be definitely expanding the RadZipLibrary online documentation. An article on how to unzip a file will be included by the end of the month. And I want to thank you for this feedback.

Also, another approach for extracting zipped files with the RadZipLibrary is:
private void UnZipFiles(string filePath, string destinationPath)
{
    FileStream fileStream = new FileStream(filePath, FileMode.Open);
 
    //extract all files from archieve
    using (ZipPackage package = ZipPackage.Open(fileStream, FileAccess.Read))
    {
        foreach (var entry in package.ZipPackageEntries)
        {
            if (entry.Attributes != FileAttributes.Directory)
            {
                SaveFile(entry, destinationPath);
            }
        }
    }
}
 
//Save the unzipped files
private static void SaveFile(ZipPackageEntry entry, string destinationPath)
{
    Stream stream = entry.OpenInputStream();
    var fileName = destinationPath + "\\" + entry.FileNameInZip;
 
    var directoryName = Path.GetDirectoryName(fileName);
 
    if (directoryName != null)
    {
        if (!Directory.Exists(directoryName))
        {
            Directory.CreateDirectory(directoryName);
        }
 
        StreamReader reader = new StreamReader(stream);
        string text = reader.ReadToEnd();
        File.WriteAllText(fileName, text);
    }
}


Regards,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Manuel
Top achievements
Rank 1
answered on 12 Apr 2013, 09:28 PM
Hello,

the example is not working for me.
I used your c# example to unzip the files.

It works fine for text files like XML or CS.
But the DLL files in the zip package are corrupted after unzip.

If I use a regular unzipper, the files are fine.

Do you have any ideas?
0
Vladislav
Telerik team
answered on 17 Apr 2013, 11:44 AM
Hi Manuel,

Unfortunately, there is a known issue for the RadZipLibrary working with ".dll" files.
It is logged here, where you can track its progress and vote for it.

We are sorry for any inconvenience caused.

Regards,
Vladislav
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Henry
Top achievements
Rank 1
answered on 15 Jan 2014, 06:57 PM
I guess, it does not work with any binary file. I tried Excel and Word files. I think the reason is that the Stream has compressed data and your code has no processing to convert this compressed data into expected format based on file extension. Everything is treated as text.

 

 

StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();

 

File.WriteAllText(fileName, text);

 

0
Kiril Vandov
Telerik team
answered on 20 Jan 2014, 12:46 PM
Hello Henry,

You can create a ZIP files and include word/excel and other files as long as you include the files with their name and extensions. For instance if you want to add a new Excel file to a zip, you need to add the files like follows: filename+extension("SampleFile.xlsx"). I have attached a sample project demonstrating the approach. Doing so the file will be added to the zip and will be compressed/decompressed as expected.

I hope this information helps.

Kind regards,
Kiril Vandov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Henry
Top achievements
Rank 1
answered on 23 Jan 2014, 01:59 PM
Kiril,
your example shows how to zip files. I've never had the problem with that. But UNZIPPING does not work. Please could you try to UNZIP your zip file using Telerik library and you will see that all files from output will be corrupted unless they are text files (.txt, .sql etc).
At least I tried with .pdf, .xls, .doc etc.

Thanks,

Henry 
0
Kiril Vandov
Telerik team
answered on 28 Jan 2014, 12:04 PM
Hello Henry,

Please excuse me if I have misunderstood your initial question.
In order to unzip a zip file using the ZipLibrary, you need to do the following steps:
1) Open the Zip file:
using (ZipPackage zipFile = ZipPackage.OpenFile(zipFilePath, FileAccess.Read))
2) Traverse through all its items in the zipFile.ZipPackageEntries collection.
foreach (var zipEntry in zipFile.ZipPackageEntries)
3) Open the decompressed stream of each entry
Stream strm = zipEntry.OpenInputStream();
4) Create new byte[] to contain the zipped data. Please note that this array should be with the size of the UncompressedFile size.
byte[] Bytes = new byte[zipEntry.UncompressedSize];
5) Read the bytes from the decompressed stream
strm.Read(Bytes, 0, (int)zipEntry.UncompressedSize);
6) Access the filename and its extension in the zipEntry.FileNameInZip property
7) Create a new file for each entry
string extractedFilePath = path + @"\New" + fileName;
using (FileStream fs = new FileStream(extractedFilePath, FileMode.Create))
{
    fs.Write(Bytes, 0, Bytes.Length);
    fs.Close();
}

I have attached a sample project demonstrating the approach. Run the attached project press the "Create ZIP file" button, then press the "Extract" button to extract the zipped files.

I hope this information helps.

Kind regards,
Kiril Vandov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Henry
Top achievements
Rank 1
answered on 28 Jan 2014, 05:53 PM
Hi Kiril,
Thank you very much! Your code works in the way it was expected to.
Tags
ZipLibrary
Asked by
Silvan
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Lee
Top achievements
Rank 1
Silvan
Top achievements
Rank 1
Tina Stancheva
Telerik team
Manuel
Top achievements
Rank 1
Vladislav
Telerik team
Henry
Top achievements
Rank 1
Kiril Vandov
Telerik team
Share this question
or