Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
GeneralRe: 'System.StackOverflowException' Pin
Simon P Stevens17-Apr-08 0:14
Simon P Stevens17-Apr-08 0:14 
GeneralQyery Builder Pin
Muneer Safi16-Apr-08 22:45
Muneer Safi16-Apr-08 22:45 
QuestionRe: Qyery Builder Pin
ali_reza_zareian17-Apr-08 0:31
ali_reza_zareian17-Apr-08 0:31 
GeneralRe: Qyery Builder Pin
Muneer Safi17-Apr-08 0:42
Muneer Safi17-Apr-08 0:42 
GeneralRe: Qyery Builder Pin
Pete O'Hanlon17-Apr-08 1:30
mvePete O'Hanlon17-Apr-08 1:30 
QuestionMultiple DataKey Pin
frian_sf16-Apr-08 22:42
frian_sf16-Apr-08 22:42 
GeneralRe: Multiple DataKey Pin
Reelix17-Apr-08 19:48
Reelix17-Apr-08 19:48 
GeneralZip multiple files Pin
JustRonald16-Apr-08 22:35
JustRonald16-Apr-08 22:35 
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.Exists(zipFileName))
                {
                    YesOrNo = (int)MessageBox.Show("Dit bestand bestaat al. Wilt u het overschrijven?", "Bestand bestaat al", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                }

                // Afhandeling van de gekozen optie
                if (YesOrNo == (int)DialogResult.No)
                {
                    Overschijven = false;
                }
                else
                {
                    try
                    {
                        Overschijven = true;

                        Crc32 crc = new Crc32();
                        ZipOutputStream zip = new ZipOutputStream(File.Create(zipFileName));

                        // waarde 0 - 9 (hierbij is 9 de beste compressie)
                        zip.SetLevel(9);

                        // Openen bestand
                        FileStream fs = File.OpenRead(fileToCompress);

                        // Ophalen van het totaal aantal bytes
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);

                        // CleanName zorgt voor een bug fix in WindowsXP SP2
                        ZipEntry entry = new ZipEntry(ZipEntry.CleanName(fileToCompress));

                        // Vullen van de entry's
                        entry.DateTime = DateTime.Now;
                        entry.ZipFileIndex = 1;
                        entry.Size = fs.Length;

                        // Bestand sluiten
                        fs.Close();

                        // Checksum resetten en updaten
                        crc.Reset();
                        crc.Update(buffer);
                        entry.Crc = crc.Value;
                        zip.PutNextEntry(entry);

                        // Het schrijven van het zip bestand
                        zip.Write(buffer, 0, buffer.Length);

                        zip.Finish();
                        zip.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(("Error: " + ex.Message), "Error");
                    }
                }
            }
        }


Can someone help me or say which part I need to change?
Any help would be greatly appriciated.

Regards,
Ronald
QuestionControlCollection Question Pin
Programm3r16-Apr-08 22:26
Programm3r16-Apr-08 22:26 
AnswerRe: ControlCollection Question Pin
Simon P Stevens16-Apr-08 23:25
Simon P Stevens16-Apr-08 23:25 
GeneralRe: ControlCollection Question Pin
Programm3r17-Apr-08 19:31
Programm3r17-Apr-08 19:31 
GeneralFetching meta data of MS Office files [modified] Pin
sachinkalse16-Apr-08 21:36
sachinkalse16-Apr-08 21:36 
GeneralRe: Fetching meta data of MS Office files Pin
Member 769142521-Feb-11 1:10
Member 769142521-Feb-11 1:10 
QuestionHow to open a XML file without validating? Pin
Hariharan210516-Apr-08 20:25
Hariharan210516-Apr-08 20:25 
AnswerRe: How to open a XML file without validating? Pin
pmarfleet16-Apr-08 22:39
pmarfleet16-Apr-08 22:39 
GeneralRe: How to open a XML file without validating? Pin
Hariharan210517-Apr-08 3:30
Hariharan210517-Apr-08 3:30 
General[Message Deleted] Pin
wwwxyz16-Apr-08 20:18
wwwxyz16-Apr-08 20:18 
GeneralRe: write to a txt file Pin
Reelix16-Apr-08 20:36
Reelix16-Apr-08 20:36 
GeneralRe: write to a txt file Pin
wwwxyz17-Apr-08 6:33
wwwxyz17-Apr-08 6:33 
GeneralRe: write to a txt file Pin
John_Adams16-Apr-08 20:40
John_Adams16-Apr-08 20:40 
GeneralRe: write to a txt file Pin
phannon8616-Apr-08 21:59
professionalphannon8616-Apr-08 21:59 
GeneralDownloading problem using c sharp Pin
Kashif Sabir16-Apr-08 19:24
Kashif Sabir16-Apr-08 19:24 
GeneralRe: Downloading problem using c sharp Pin
Expert Coming16-Apr-08 19:39
Expert Coming16-Apr-08 19:39 
QuestionTechnology Choice? Pin
Expert Coming16-Apr-08 19:23
Expert Coming16-Apr-08 19:23 
AnswerRe: Technology Choice? Pin
Thomas Stockwell17-Apr-08 1:16
professionalThomas Stockwell17-Apr-08 1:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.