Ionic.Zip.ZipFile.AddItem C# (CSharp) Method

AddItem() public method

Adds an item, either a file or a directory, to a zip file archive, explicitly specifying the directory path to be used in the archive.

If adding a directory, the add is recursive on all files and subdirectories contained within it.

The name of the item may be a relative path or a fully-qualified path. The item added by this call to the ZipFile is not read from the disk nor written to the zip file archive until the application calls Save() on the ZipFile.

This version of the method allows the caller to explicitly specify the directory path to be used in the archive, which would override the "natural" path of the filesystem file.

Encryption will be used on the file data if the Password has been set on the ZipFile object, prior to calling this method.

For ZipFile properties including Encryption, , SetCompression, , ExtractExistingFile, ZipErrorAction, and CompressionLevel, their respective values at the time of this call will be applied to the ZipEntry added.

/// Thrown if the file or directory passed in does not exist. ///
public AddItem ( String fileOrDirectoryName, String directoryPathInArchive ) : ZipEntry
fileOrDirectoryName String the name of the file or directory to add. ///
directoryPathInArchive String /// The name of the directory path to use within the zip archive. This path /// need not refer to an extant directory in the current filesystem. If the /// files within the zip are later extracted, this is the path used for the /// extracted file. Passing null (Nothing in VB) will use the /// path on the fileOrDirectoryName. Passing the empty string ("") will /// insert the item at the root path within the archive. ///
return ZipEntry
        public ZipEntry AddItem(String fileOrDirectoryName, String directoryPathInArchive)
        {
            if (File.Exists(fileOrDirectoryName))
                return AddFile(fileOrDirectoryName, directoryPathInArchive);

            if (Directory.Exists(fileOrDirectoryName))
                return AddDirectory(fileOrDirectoryName, directoryPathInArchive);

            throw new FileNotFoundException(String.Format("That file or directory ({0}) does not exist!",
                                                          fileOrDirectoryName));
        }

Same methods

ZipFile::AddItem ( string fileOrDirectoryName ) : ZipEntry

Usage Example

        static string CreateFileToUpload(IList<string> files)
        {
            if (files == null || files.Count == 0)
            {
                Console.WriteLine("No files were supplied");
                Environment.Exit(1);
            }

            string temporaryFile = Path.GetTempFileName() + ".zip";

            using (var zip = new ZipFile())
            {
                foreach (string file in files)
                {
                    if (File.Exists(file) || Directory.Exists(file))
                    {
                        zip.AddItem(file);
                    }
                    else if (file.Contains("*") || file.Contains("?"))
                    {
                        string parent = Path.GetDirectoryName(file);

                        if (parent == null)
                        {
                            Console.WriteLine("Unabled to find file or folder: {0}", file);
                            Environment.Exit(10);
                        }

                        string wildCard = Path.GetFileName(file);

                        if (wildCard == null)
                        {
                            Console.WriteLine("Unabled to find file or folder: {0}", file);
                            Environment.Exit(10);
                        }

                        foreach (FileInfo fileInPattern in new DirectoryInfo(parent).GetFiles(wildCard))
                        {
                            zip.AddItem(fileInPattern.FullName);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Unabled to find file or folder: {0}", file);
                        Environment.Exit(10);
                    }
                }

                zip.Save(temporaryFile);
            }

            return temporaryFile;
        }
All Usage Examples Of Ionic.Zip.ZipFile::AddItem