Zip multiple files
C#
1
Posts
1
Posters
0
Views
1
Watching
-
Hi, I created an application which will zip .txt files into .zip For each textfile I selected in my listbox it creates one zip file. But I want to modify it now so all files I select would come in one zip file. This is a part of my form.cs file:
public Form1()
{
InitializeComponent();
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.DataSource = ClassFile.GetFiles(@"C:\");
}private void button1_Click(object sender, EventArgs e)
{
/* Declaratie */
ArrayList bestanden;
string StoragePath;
string OldFile;
string NewFile;
string[] split;
int i;/\* Initialisatie \*/ bestanden = new ArrayList(); StoragePath = @"C:\\"; /\* Code \*/ for (i = 0; i < listBox1.SelectedItems.Count; i++) { split = listBox1.SelectedItems\[i\].ToString().Split('.'); bestanden.Add(split\[0\]); } listBox2.Items.Clear(); foreach (string bestand in bestanden) { OldFile = StoragePath + bestand + ".txt"; NewFile = StoragePath + bestand + ".zip"; listBox2.Items.Add("Tekst bestand: " + OldFile); ClassFile.FileToZip(StoragePath, OldFile, NewFile); if (ClassFile.Overschijven == true) { listBox2.Items.Add("ZIP conversie succesvol: " + NewFile); } else { listBox2.Items.Add("ZIP conversie mislukt: " + OldFile); } }
}
This is my class file:
public ArrayList GetFiles(string dir)
{
/* Declaratie */
ArrayList list;
DirectoryInfo di;
FileInfo[] rgFiles;/\* Initialisatie \*/ list = new ArrayList(); di = new DirectoryInfo(dir); rgFiles = di.GetFiles("\*.txt"); /\* Code \*/ foreach(FileInfo fi in rgFiles) { list.Add(fi.Name); } return list; } public void FileToZip(string zipFileStoragePath, string fileToCompress, string zipFileName) { // Controle of het bestand wel bestaat if (File.Exists(fileToCompress) == false) { MessageBox.Show("Bestand niet gevonden.", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { // Controle of het zip bestand al bestaat if (File.Exist