How can I compress into a .zip file and then delete the original files

1

I have a program that calls a stored procedure which works well.

The result of this, gives me 10 files with the name of the different projects.

What I'm trying to do is to be able to compress those 10 files and make a single .ZIP file that contains these files and then all the 10 original .CSV files can be deleted.

How could I do this?

Here is my code:

List<string> projectName = new List<string>();
List<string> category = new List<string>();
List<string> Subcategory = new List<string>();

int count = xml.GetElementsByTagName("projectName").Count;

for (int i = 0; i < count; ++i)
{
    projectName.Add(xml.GetElementsByTagName("projectName")[i].InnerText);
    category.Add(xml.GetElementsByTagName("category")[i].InnerText);
    Subcategory.Add(xml.GetElementsByTagName("Subcategory")[i].InnerText);
}

for (int i = 0; i < count; ++i)
{
    string outCsvFile = string.Format(@"C:\temp\{0}.csv", category[i] + " " + Subcategory[i]);

    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandType = CommandType.StoredProcedure;
    sqlCmd.Connection = conn;
    sqlCmd.CommandText = "testStoredProcedure";
    sqlCmd.Parameters.AddWithValue("@ProjectTitle", projectName[i]);
    sqlCmd.Parameters.AddWithValue("@Category", category[i]);
    sqlCmd.Parameters.AddWithValue("@SubCategory", Subcategory[i]);
    sqlCmd.Parameters.AddWithValue("@TaskType", "");
    sqlCmd.Parameters.AddWithValue("@TaskStatus", "");
    sqlCmd.Parameters.AddWithValue("@IsBaseLanguage", 1);
    sqlCmd.Parameters.AddWithValue("@RaterDemographics", "FirtName,LastName,Email,Adress");

    conn.Open();

    SqlDataReader reader = sqlCmd.ExecuteReader();
    using (StreamWriter file = new StreamWriter(outCsvFile))
    {
        List<string> items = new List<string>();
        for (int j = 0; j <= 5; j++)
        {
            items.Add(reader.GetName(j));
        }
        file.WriteLine("\"" + string.Join("\",\"", items) + "\"");


        while (reader.Read())
        {
            items = new List<string>();
            for (int j = 0; j <= 5; j++)
            {
                items.Add(reader[j].ToString());
            }
            file.WriteLine("\"" + string.Join("\",\"", items) + "\"");

        }

    }
    conn.Close();

}
    
asked by A arancibia 18.08.2016 в 20:10
source

2 answers

0

To be able to compress you could use the classes included in .net

How to: Compress and extract files

how many with the class ZipFile to create the zip

Now the issue is how to generate the zip without the files are blocked, for that surely you should use pretty well the using , this way when you finish putting the zip to exit using is released to be able to delete the file

Zip Archives in C # 5.0 (.NET 4.5)

important is to analyze how you use

 using (ZipArchive zipArchive =   ZipFile.Open(@"C:\Archive.zip", ZipArchiveMode.Read)){ ...

there is the using that I comment so that when you finish building the zip files are released, this way you can delete them

    
answered by 18.08.2016 / 23:25
source
0

After you have compressed the files you can add a loop to delete them:

for (int i = 0; i < count; ++i)
{        
  System.IO.File.Delete(@"C:\temp\{0}.csv", category[i] + " " + Subcategory[i])
}
    
answered by 18.08.2016 в 22:45