Example #1
0
        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();
        }
 /// <summary>Copies the elements of an array to the end of the <see cref='OrganicBit.Zip.ZipEntryCollection'/>.</summary>
 /// <param name='value'>An array of type <see cref='OrganicBit.Zip.ZipEntry'/> containing the objects to add to the collection.</param>
 /// <returns>None.</returns>
 /// <seealso cref='OrganicBit.Zip.ZipEntryCollection.Add'/>
 public void AddRange(ZipEntry[] value)
 {
     for (int i = 0; (i < value.Length); i = (i + 1)) {
         this.Add(value[i]);
     }
 }
 /// <summary>Adds a <see cref='OrganicBit.Zip.ZipEntry'/> with the specified value to the <see cref='OrganicBit.Zip.ZipEntryCollection'/>.</summary>
 /// <param name='value'>The <see cref='OrganicBit.Zip.ZipEntry'/> to add.</param>
 /// <returns>The index at which the new element was inserted.</returns>
 /// <seealso cref='OrganicBit.Zip.ZipEntryCollection.AddRange'/>
 public int Add(ZipEntry value)
 {
     return List.Add(value);
 }
 /// <summary>Initializes a new instance of <see cref='OrganicBit.Zip.ZipEntryCollection'/> containing any array of <see cref='OrganicBit.Zip.ZipEntry'/> objects.</summary>
 /// <param name='value'>A array of <see cref='OrganicBit.Zip.ZipEntry'/> objects with which to intialize the collection.</param>
 public ZipEntryCollection(ZipEntry[] value)
 {
     this.AddRange(value);
 }
 /// <summary>Removes a specific <see cref='OrganicBit.Zip.ZipEntry'/> from the <see cref='OrganicBit.Zip.ZipEntryCollection'/>.</summary>
 /// <param name='value'>The <see cref='OrganicBit.Zip.ZipEntry'/> to remove from the <see cref='OrganicBit.Zip.ZipEntryCollection'/> .</param>
 /// <returns><para>None.</para></returns>
 /// <exception cref='System.ArgumentException'><paramref name='value'/> is not found in the Collection. </exception>
 public void Remove(ZipEntry value)
 {
     List.Remove(value);
 }
 /// <summary>Inserts a <see cref='OrganicBit.Zip.ZipEntry'/> into the <see cref='OrganicBit.Zip.ZipEntryCollection'/> at the specified index.</summary>
 /// <param name='index'>The zero-based index where <paramref name='value'/> should be inserted.</param>
 /// <param name=' value'>The <see cref='OrganicBit.Zip.ZipEntry'/> to insert.</param>
 /// <returns><para>None.</para></returns>
 /// <seealso cref='OrganicBit.Zip.ZipEntryCollection.Add'/>
 public void Insert(int index, ZipEntry value)
 {
     List.Insert(index, value);
 }
 /// <summary>Returns the index of a <see cref='OrganicBit.Zip.ZipEntry'/> in the <see cref='OrganicBit.Zip.ZipEntryCollection'/>.</summary>
 /// <param name='value'>The <see cref='OrganicBit.Zip.ZipEntry'/> to locate.</param>
 /// <returns>
 /// <para>The index of the <see cref='OrganicBit.Zip.ZipEntry'/> of <paramref name='value'/> in the 
 /// <see cref='OrganicBit.Zip.ZipEntryCollection'/>, if found; otherwise, -1.</para>
 /// </returns>
 /// <seealso cref='OrganicBit.Zip.ZipEntryCollection.Contains'/>
 public int IndexOf(ZipEntry value)
 {
     return List.IndexOf(value);
 }
 /// <summary>Copies the <see cref='OrganicBit.Zip.ZipEntryCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the specified index.</summary>
 /// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='OrganicBit.Zip.ZipEntryCollection'/> .</para></param>
 /// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
 /// <returns>None.</returns>
 /// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='OrganicBit.Zip.ZipEntryCollection'/> is greater than the available space between <paramref name='arrayIndex'/> and the end of <paramref name='array'/>.</para></exception>
 /// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
 /// <exception cref='System.ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>
 /// <seealso cref='System.Array'/>
 public void CopyTo(ZipEntry[] array, int index)
 {
     List.CopyTo(array, index);
 }
 /// <summary>Gets a value indicating whether the <see cref='OrganicBit.Zip.ZipEntryCollection'/> contains the specified <see cref='OrganicBit.Zip.ZipEntry'/>.</summary>
 /// <param name='value'>The <see cref='OrganicBit.Zip.ZipEntry'/> to locate.</param>
 /// <returns>
 /// <para><see langword='true'/> if the <see cref='OrganicBit.Zip.ZipEntry'/> is contained in the collection; 
 ///   otherwise, <see langword='false'/>.</para>
 /// </returns>
 /// <seealso cref='OrganicBit.Zip.ZipEntryCollection.IndexOf'/>
 public bool Contains(ZipEntry value)
 {
     return List.Contains(value);
 }
 private void OpenEntry()
 {
     _current = new ZipEntry(_handle);
     int result = ZipLib.unzOpenCurrentFile(_handle);
     if (result < 0) {
         _current = null;
         throw new ZipException("Could not open entry for reading.");
     }
     _entryOpen = true;
 }
 /// <summary>Move to just before the first entry in the zip directory.</summary>
 public void Reset()
 {
     CloseEntry();
     _current = null;
 }
        /// <summary>Advances the enumerator to the next element of the collection.</summary>
        /// <summary>Sets <see cref="Current"/> to the next zip entry.</summary>
        /// <returns><c>true</c> if the next entry is not <c>null</c>; otherwise <c>false</c>.</returns>
        public bool MoveNext()
        {
            // close any open entry
            CloseEntry();

            int result;
            if (_current == null) {
                result = ZipLib.unzGoToFirstFile(_handle);
            } else {
                result = ZipLib.unzGoToNextFile(_handle);
            }
            if (result < 0) {
                // last entry found - not an exceptional case
                _current = null;
            } else {
                // entry found
                OpenEntry();
            }

            return (_current != null);
        }
        /// <summary>Creates a new zip entry in the directory and positions the stream to the start of the entry data.</summary>
        /// <param name="entry">The zip entry to be written.</param>
        /// <remarks>Closes the current entry if still active.</remarks>
        public void AddEntry(ZipEntry entry)
        {
            ZipFileEntryInfo info;
            info.DateTime = entry.ModifiedTime;

            int result;
            unsafe {
                byte[] extra = null;
                uint extraLength = 0;
                if (entry.ExtraField != null) {
                    extra = entry.ExtraField;
                    extraLength = (uint) entry.ExtraField.Length;
                }

                result = ZipLib.zipOpenNewFileInZip(
                    _handle,
                    entry.Name,
                    &info,
                    extra,
                    extraLength,
                    null, 0,
                    entry.Comment,
                    (int) entry.Method,
                    entry.Level);
            }
            _entryOpen = true;
        }
Example #14
0
        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;
        }