How to extract a file

13 posts, 0 answers
  1. Silvan
    Silvan avatar
    10 posts
    Member since:
    Jun 2009

    Posted 27 Oct 2011 Link to this post

    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
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 01 Nov 2011 Link to this post

    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 >>

  3. Lee Weisenberger
    Lee Weisenberger avatar
    72 posts
    Member since:
    Nov 2009

    Posted 17 Nov 2011 Link to this post

    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?
  4. Silvan
    Silvan avatar
    10 posts
    Member since:
    Jun 2009

    Posted 18 Nov 2011 Link to this post

    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.
  5. Lee Weisenberger
    Lee Weisenberger avatar
    72 posts
    Member since:
    Nov 2009

    Posted 18 Nov 2011 Link to this post

    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.
  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 22 Nov 2011 Link to this post

    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 >>

  7. Manuel
    Manuel avatar
    43 posts
    Member since:
    Jul 2010

    Posted 12 Apr 2013 Link to this post

    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?
  8. Vladislav
    Admin
    Vladislav avatar
    276 posts

    Posted 17 Apr 2013 Link to this post

    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.

  9. Henry
    Henry avatar
    3 posts
    Member since:
    Sep 2012

    Posted 15 Jan 2014 Link to this post

    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);

     

  10. Kiril Vandov
    Admin
    Kiril Vandov avatar
    324 posts

    Posted 20 Jan 2014 Link to this post

    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 >>
  11. Henry
    Henry avatar
    3 posts
    Member since:
    Sep 2012

    Posted 23 Jan 2014 Link to this post

    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 
  12. Kiril Vandov
    Admin
    Kiril Vandov avatar
    324 posts

    Posted 28 Jan 2014 Link to this post

    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 >>
  13. Henry
    Henry avatar
    3 posts
    Member since:
    Sep 2012

    Posted 28 Jan 2014 in reply to Kiril Vandov Link to this post

    Hi Kiril,
    Thank you very much! Your code works in the way it was expected to.
Back to Top