Hi sani.ss501,
You need to extract the file from Zip and save the details in database.
For extracting file from Zip i am using Ionic.Zip.Reduced library.
For more details refer below article.
For upload files in database refer below article.
Using both the article i have created an example. Refer below example.
HTML
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
<hr />
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FileId" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ImageField DataImageUrlField="Path" HeaderText="Image" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using Ionic.Zip;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Imports Ionic.Zip
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Files", conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gvFiles.DataSource = dt;
gvFiles.DataBind();
}
}
}
}
protected void OnSave(object sender, EventArgs e)
{
string extractPath = Server.MapPath("~/Files/");
if (!Directory.Exists(extractPath))
{
Directory.CreateDirectory(extractPath);
}
using (ZipFile zip = ZipFile.Read(fuUpload.PostedFile.InputStream))
{
zip.ExtractAll(extractPath, ExtractExistingFileAction.DoNotOverwrite);
List<ZipEntry> files = zip.Entries.ToList();
foreach (ZipEntry file in files)
{
// Insert file path and name into database.
this.Insert(file.FileName);
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Insert(string fileName)
{
string filePath = string.Format("~/Files/{0}", fileName);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO Files VALUES(@Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Files", conn)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvFiles.DataSource = dt
gvFiles.DataBind()
End Using
End Using
End If
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim extractPath As String = Server.MapPath("~/Files/")
If Not Directory.Exists(extractPath) Then
Directory.CreateDirectory(extractPath)
End If
Using zip As ZipFile = ZipFile.Read(fuUpload.PostedFile.InputStream)
zip.ExtractAll(extractPath, ExtractExistingFileAction.DoNotOverwrite)
Dim files As List(Of ZipEntry) = zip.Entries.ToList()
For Each file As ZipEntry In files
Me.Insert(file.FileName)
Next
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub Insert(ByVal fileName As String)
Dim filePath As String = String.Format("~/Files/{0}", fileName)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Dim sql As String = "INSERT INTO Files VALUES(@Name, @Path)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@Path", filePath)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End Sub
Screenshot