Zip files and send to sever
-
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(); }
-
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(); }
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 )