Zippen mit Struktur Panne

lusiphur

Mitglied
Hallo,

Ich habe versucht den code aus dem beitrag


Zu adaptieren und um eine Pfadeingabe mittesl Save file Dialog erweitert doch irgenetwas mache ich Falsch da ich immer nur Lere Zip files erstelle ohne inhalt obwol im Ordner eindeutig inhalt verhanden ist

Mfg
Lusiphur

Code:
string parentPath;
        private void button1_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Backup Dateien | *.zip";
            saveFileDialog1.DefaultExt = "*.zip";
            saveFileDialog1.ShowDialog();
            string path3=saveFileDialog1.FileName.ToString();

            ZipOutputStream s = new ZipOutputStream(File.Create(path3));
            s.SetLevel(1);
            DirectoryInfo maindir = new DirectoryInfo("C:\\Projekt\\");
            parentPath = maindir.Parent.FullName + @"\";
            MessageBox.Show(parentPath);

            GoSubdirs(ref s, statics.Projektpfad);

            s.Close();
        }
    
 

        private void GoSubdirs(ref ZipOutputStream s, string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            foreach (DirectoryInfo subdir in dir.GetDirectories())
            {
                GoSubdirs(ref s, subdir.FullName);
            }
            foreach (FileInfo fi in dir.GetFiles())
            {
                FileStream fs = File.OpenRead(fi.FullName);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry(dir.FullName.Replace(parentPath, "") + "/" + fi.Name);
                entry.Size = fs.Length;
                fs.Close();
                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
            }
        }
 
Hallo.

Was mir auf die Schnelle aufgefallen ist:

C#:
DirectoryInfo maindir = new DirectoryInfo("C:\\Projekt\\");
parentPath = maindir.Parent.FullName + @"\";
MessageBox.Show(parentPath);

Du bastelst dir da einen Pfad zusammen.. und zeigst ihn dir via MessageBox an.

C#:
GoSubdirs(ref s, statics.Projektpfad);

Benutzt dann aber doch was anderes. Stimmt der Pfad?

Im übrigen hat AsterixAoH eine kleine Ausbesserung am Code gemacht, die du auch übernehmen solltest.


lg, Alex
 
das Problem ist nun das ich zwar ein Zipfile der Richtigen größe erhalte 68 K aber es ist ohne Daten ?

was ist da nun Los
Die Änderungen die Sie anmerkten beziehen sich nur auf das Entpacken diese abe ich noch nichteinmal bearbeitet
Code:
 public  string parentPath;
        private void button1_Click(object sender, EventArgs e)
        {
         
            
	ZipOutputStream s = new ZipOutputStream(File.Create(@"C:\testzip.zip"));
	s.SetLevel(1);
	DirectoryInfo maindir = new DirectoryInfo("C:\\Projekt");
	parentPath = maindir.Parent.FullName + @"\";
	MessageBox.Show(parentPath);

	GoSubdirs(ref s,"C:\\Projekt");

	s.Close();
	

            
        }
    private void GoSubdirs(ref ZipOutputStream s, string path)
{
	DirectoryInfo dir = new DirectoryInfo(path);
	foreach (DirectoryInfo subdir in dir.GetDirectories())
	{
		GoSubdirs(ref s, subdir.FullName);
	}
	foreach (FileInfo fi in dir.GetFiles())
	{
		FileStream fs = File.OpenRead(fi.FullName);
			
		byte[] buffer = new byte[fs.Length];
		fs.Read(buffer, 0, buffer.Length);
		ZipEntry entry = new ZipEntry(dir.FullName.Replace(parentPath,"") + "/" + fi.Name);
		entry.Size = fs.Length;
		fs.Close();
		s.PutNextEntry(entry);
		s.Write(buffer, 0, buffer.Length);
        s.Flush();
	}
 
Zurück