Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
:( Hi guys,
I'm struggling with logic here I have exported a csv file from database but now I want to take that csv file zip it then save it to a database again.

Your help will be very much appreciated.
Regards,
A
Posted
Comments
senguptaamlan 23-Sep-10 8:27am    
please, let us know what you have done with respect to coding and pin point the actual implementation difficulties that you are facing

1 solution

You can use the following links to select a zip method you like.

Folder zipper using GZipStream:
http://www.eggheadcafe.com/tutorials/aspnet/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-files-with-c.aspx[^]

Or a component like SharpZipLib:
http://www.icsharpcode.net/OpenSource/SharpZipLib/?[^]

Here you have some code for storing a file into a database. (zip or whatever format you want)

C#
try
{
    string constr = "SERVER=localhost;DATABASE=master;pwd=sa;uid=sa";
    using (SqlConnection con = new SqlConnection(constr))
    {
         con.Open();
         string path = @"C:\TEMP\compressed.zip";
         byte[] bytearr = File.ReadAllBytes(path);
         string query = "Insert into files values(@file1)";
         using (SqlCommand com = new SqlCommand(query,con))
         {
               com.Parameters.Add(new SqlParameter("@file1", bytearr));
               com.ExecuteNonQuery();
         }
    }
}
catch (Exception ex)
{
  throw ex;
}


The example above now takes a file and stores it into the table "files" as bytearray but as you may notice the GZipStream stores the compressed data into a stream that can easily be a memory stream instead of a file stream. This would avoid the use of a temporary file but you would do some changing to the folder zipper code.

Good luck!
 
Share this answer
 
Comments
JGraduate 23-Sep-10 10:05am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
JGraduate 23-Sep-10 10:09am    
I will work on it and I will let you know how it went.

Many thanks
A

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900