Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Zip files and send to sever

Zip files and send to sever

Scheduled Pinned Locked Moved C#
sysadminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    minniemooo
    wrote on last edited by
    #1

    My code below gets files from a list box and Zip those files into one Zip file. My propble is: This works perfectly when i log on to the server and do this operation on the sever itself.(it is an Intranet). But when i do this on my local machine which is on the same network as the server, it only creates the Zip file but its empty. Can someone please help. //Zip txtsave.Text = "c:\\TempUpload"; System.IO.Directory.CreateDirectory(txtsave.Text); if(ListBox1.Items.Count < 1) { listerror.Text = "Please add Files to the list Box and Press Confirm"; return; } if (txtsave.Text == string.Empty) { listerror.Text = "save folder not selected"; return; } string[] sTemp = txtsave.Text.Split('\\'); string sZipFileName = sTemp[sTemp.Length - 1].ToString(); FileInfo fi = new FileInfo(txtsave.Text + "\\" + sZipFileName + ".zip"); if(!System.IO.Directory.Exists(txtsave.Text + "\\TempZipFile\\")) { System.IO.Directory.CreateDirectory(txtsave.Text + "\\TempZipFile\\" + ".zip"); } string sTargetFolderPath = (txtsave.Text + "\\TempZipFile\\"); for (int i =0; i< ListBox1.Items.Count;i++) { string filepath = ListBox1.Items[i].ToString(); FileInfo fi2 = new FileInfo(filepath); if(fi2.Exists) { try { fi2.CopyTo(sTargetFolderPath + fi2.Name, true); } catch { System.IO.Directory.Delete(sTargetFolderPath); listerror.Text = "Error on Files"; return; } } } try { string[] filenames = Directory.GetFiles(sTargetFolderPath); using (ZipOutputStream s = new ZipOutputStream(File.Create(txtsave.Text + "\\" + sZipFileName + ".zip"))) { s.SetLevel(9); byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceByte; do { sourceByte = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceByte); } while(sourceByte > 0); } } s.Finish(); s.Close(); } System.IO.Directory.Delete(txtsave.Text + "\\TempZipFile\\", true); } catch(Exception ex) { listerror.Text = ex.Message.ToString(); }

    C 1 Reply Last reply
    0
    • M minniemooo

      My code below gets files from a list box and Zip those files into one Zip file. My propble is: This works perfectly when i log on to the server and do this operation on the sever itself.(it is an Intranet). But when i do this on my local machine which is on the same network as the server, it only creates the Zip file but its empty. Can someone please help. //Zip txtsave.Text = "c:\\TempUpload"; System.IO.Directory.CreateDirectory(txtsave.Text); if(ListBox1.Items.Count < 1) { listerror.Text = "Please add Files to the list Box and Press Confirm"; return; } if (txtsave.Text == string.Empty) { listerror.Text = "save folder not selected"; return; } string[] sTemp = txtsave.Text.Split('\\'); string sZipFileName = sTemp[sTemp.Length - 1].ToString(); FileInfo fi = new FileInfo(txtsave.Text + "\\" + sZipFileName + ".zip"); if(!System.IO.Directory.Exists(txtsave.Text + "\\TempZipFile\\")) { System.IO.Directory.CreateDirectory(txtsave.Text + "\\TempZipFile\\" + ".zip"); } string sTargetFolderPath = (txtsave.Text + "\\TempZipFile\\"); for (int i =0; i< ListBox1.Items.Count;i++) { string filepath = ListBox1.Items[i].ToString(); FileInfo fi2 = new FileInfo(filepath); if(fi2.Exists) { try { fi2.CopyTo(sTargetFolderPath + fi2.Name, true); } catch { System.IO.Directory.Delete(sTargetFolderPath); listerror.Text = "Error on Files"; return; } } } try { string[] filenames = Directory.GetFiles(sTargetFolderPath); using (ZipOutputStream s = new ZipOutputStream(File.Create(txtsave.Text + "\\" + sZipFileName + ".zip"))) { s.SetLevel(9); byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceByte; do { sourceByte = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceByte); } while(sourceByte > 0); } } s.Finish(); s.Close(); } System.IO.Directory.Delete(txtsave.Text + "\\TempZipFile\\", true); } catch(Exception ex) { listerror.Text = ex.Message.ToString(); }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Are you connecting to the server from your local machine ? Is this in ASP.NET ? Seems to me like you wrote code expecting it to run locally, but it runs on the server.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups