New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Read Data from Zip File

You can select only one ZIP file at a time and upload it on the server.





  • Demo Configurator

By pressing the "Show zipped files' details in RadGrid" button the data from the zip file will be read and the details will be shown in Telerik’s ASP.NET Grid control. If you select the “Keep the info about my uploaded items upon a next upload” checkbox, the data about your previously uploaded items will be preserved upon a consecutive upload.

The example demonstrates how to read all the data from a ZIP file simultaneously and display the information about the compressed files in Telerik’s ASP.NET Grid - name, original and compressed size. In order to load data from uploaded ZIP files, you can take advantage of the ZipArchive and ZipArchiveEntry classes and their methods. For example:

     
        UploadedFile file = AsyncUpload1.UploadedFiles[0];
        using (ZipArchive archive = new ZipArchive(file.InputStream))
        {
            List<ZipArchiveEntry> allEntries = archive.Entries.ToList();
            foreach (ZipArchiveEntry entry in allEntries)
            {
                
                FileHelper fileHelper = new FileHelper();
                 
                fileHelper.CompressedSize = entry.CompressedLength;
                fileHelper.UncompressedSize = entry.Length;
                fileHelper.FileNameInZip = entry.Name;
                ...
                byte[] data = new byte[entry.Length];
                Stream entryStream = entry.Open();
                BinaryReader reader = new BinaryReader(entryStream);
                reader.Read(data, 0, data.Length);
                     
                fileHelper.Data = data;
                Files.Add(fileHelper);
            }
        }
  • DefaultCS.aspx
  • DefaultCS.aspx.cs
    • DefaultCS.aspx.cs
    • FileHelper.cs
  • scripts.js
  • styles.css
<%@ Page Title="" Language="C#"  AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="ZipLibrary_Examples_ReadDataFromZipFile_DefaultCS" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <script src="scripts.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <qsf:MessageBox ID="InformationBox1" Icon="Info" Type="Info" runat="server">
        <p>
            You can select only one ZIP file at a time and upload it on the server.
        </p>
    </qsf:MessageBox>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="conditionalPostback">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="Panel1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="Panel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="CheckBox1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="CheckBox1" />
                    <telerik:AjaxUpdatedControl ControlID="Panel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1"></telerik:RadAjaxLoadingPanel>
    <asp:Panel runat="server" ID="Panel1" CssClass="demo-container size-wide">
        <telerik:RadAsyncUpload
            runat="server" ID="AsyncUpload1" MaxFileInputsCount="1" AllowedFileExtensions="zip" />
        <br />
        <br />
        <telerik:RadButton RenderMode="Lightweight" Text="Show zipped files' details in RadGrid" ID="Button1" OnClick="Button1_Click" runat="server"></telerik:RadButton>
        <br />
        <br />
        <telerik:RadGrid RenderMode="Lightweight" OnItemCommand="RadGrid1_ItemCommand" runat="server" ID="RadGrid1"
            PageSize="10" AllowSorting="true" AutoGenerateColumns="false"
            AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource">
            <MasterTableView DataKeyNames="ID">
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="FileExtensionIconColumn">
                        <HeaderStyle Width="20px" />
                        <ItemTemplate>
                            <asp:Image runat="server" ImageUrl='<%# Eval("IconPath") %>' />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="FileNameInZip" HeaderText="File Name"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CompressedSize" HeaderText="Compressed Size"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="UncompressedSize" HeaderText="Uncompressed Size"></telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn UniqueName="DownloadTemplateColumn">
                        <HeaderStyle Width="40px" />
                        <ItemTemplate>
                            <telerik:RadButton RenderMode="Lightweight" runat="server" ID="RadButton1" CommandName="Custom" Text="Download file"></telerik:RadButton>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </asp:Panel>

    <qsf:ConfiguratorPanel ID="ConfiguratorPanel1" runat="server" Title="Demo Configurator">
        <Views>
            <qsf:View>
                <ul class="fb-group">
                    <li>
                        <span class="checkbox">
                            <asp:CheckBox ID="CheckBox1" AutoPostBack="true" runat="server" Text="Keep the info about my uploaded items upon a next upload" /></span>
                    </li>
                </ul>
            </qsf:View>
        </Views>
    </qsf:ConfiguratorPanel>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance