public static void Add(string zipFileName, string[] entryPatterns) { string currentDirectory = Directory.GetCurrentDirectory(); Console.WriteLine("Creating {0}", zipFileName); ZipWriter writer = new ZipWriter(zipFileName); // buffer to hold temp bytes byte[] buffer = new byte[4096]; int byteCount; // add files to archive foreach (string pattern in entryPatterns) { foreach (string path in Directory.GetFiles(currentDirectory, pattern)) { string fileName = Path.GetFileName(path); Console.Write("Adding {0}", fileName); ZipEntry entry = new ZipEntry(fileName); entry.ModifiedTime = File.GetLastWriteTime(fileName); entry.Comment = "local file comment"; writer.AddEntry(entry); FileStream reader = File.OpenRead(entry.Name); while ((byteCount = reader.Read(buffer, 0, buffer.Length)) > 0) { Console.Write("."); writer.Write(buffer, 0, byteCount); } reader.Close(); Console.WriteLine(); } } writer.Close(); }
public string ZipFiles(ArrayList FileList, string PhysZipPath, string ZipFileName) { //'If there are no checked files then return string PhysZipFilePath = ""; if (FileList.Count == 0) { return String.Empty; } //'If the directory to place Zip file doesn't exist, create it if (!Directory.Exists(PhysZipPath)) { Directory.CreateDirectory(PhysZipPath); } //'Figure out full file name and path for the Zip file if (ZipFileName == String.Empty) { //'If name of the Zip file is not provided PhysZipFilePath = new Ultils().AddBackSlashInTheEndIfNotPresent(PhysZipPath) + Path.GetFileNameWithoutExtension(Convert.ToString(FileList[0])) + ".zip"; int i = 0; //'If this name already exists, create an unique name while (File.Exists(PhysZipFilePath)) { i += 1; PhysZipFilePath = new Ultils().AddBackSlashInTheEndIfNotPresent(PhysZipFilePath) + Path.GetFileNameWithoutExtension(Convert.ToString(FileList[0]) + "(" + i + ")" + ".zip"); } } else { //'If name of the Zip file is provided, //'just construct physical path for the file PhysZipFilePath = new Ultils().AddBackSlashInTheEndIfNotPresent(PhysZipPath) + ZipFileName; } //'Create an empty zip file ZipWriter writer = new ZipWriter(PhysZipFilePath); //'Prepare a buffer to write Byte[] buffer = new Byte[4096]; int byteCount = 0; FileStream reader = null; string filezipstore = ""; ZipEntry entry; int k = 0; try { foreach (string Item in FileList) { k += 1; filezipstore = Item.Replace(Server.MapPath(currentPath), ""); entry = new ZipEntry(filezipstore); entry.ModifiedTime = File.GetLastWriteTime(Item); writer.AddEntry(entry); try { reader = File.OpenRead(Item); //'Read from file until the end and write it into zip file byteCount = reader.Read(buffer, 0, buffer.Length); while (byteCount > 0) { //'Write to file compress bytes writer.Write(buffer, 0, byteCount); byteCount = reader.Read(buffer, 0, buffer.Length); } } finally { //'Close the file reader.Close(); } } } catch (UnauthorizedAccessException ex) { ShowInfo((string)this.GetGlobalResourceObject("Lang","LOC_ERROR_ZIP_CANNOTCREATE_ACCESSDENIED"), MessageType.ErrorMsg); return ""; } catch (ArgumentException ex) { ShowInfo((string)this.GetGlobalResourceObject("Lang","LOC_ERROR_ZIP_CANNOTCREATE_WRONGNAME"), MessageType.ErrorMsg); return ""; } catch (Exception ex) { ShowInfo((string)this.GetGlobalResourceObject("Lang","LOC_ERROR_COPY_CANNOTCREATE") + " " + ex.Message, MessageType.ErrorMsg); return ""; } finally { //'Close the zip file writer.Close(); } return PhysZipFilePath; }