示例#1
0
        /// <summary>
        /// Removes one of many files in storage. It creates a new Zip file.
        /// </summary>
        /// <param name="_zip">Reference to the current Zip object</param>
        /// <param name="_zfes">List of Entries to remove from storage</param>
        /// <returns>True if success, false if not</returns>
        /// <remarks>This method only works for storage of type FileStream</remarks>
        public static bool RemoveEntries(ref FwkZip _zip, List <ZipFileEntry> _zfes)
        {
            if (!(_zip.ZipFileStream is FileStream))
            {
                throw new InvalidOperationException("RemoveEntries is allowed just over streams of type FileStream");
            }


            //Get full list of entries
            List <ZipFileEntry> fullList = _zip.ReadCentralDir();

            //In order to delete we need to create a copy of the zip file excluding the selected items
            string tempZipName   = Path.GetTempFileName();
            string tempEntryName = Path.GetTempFileName();

            try
            {
                FwkZip tempZip = FwkZip.Create(tempZipName, string.Empty);

                foreach (ZipFileEntry zfe in fullList)
                {
                    if (!_zfes.Contains(zfe))
                    {
                        if (_zip.ExtractFile(zfe, tempEntryName))
                        {
                            tempZip.AddFile(zfe.Method, tempEntryName, zfe.FilenameInZip, zfe.Comment);
                        }
                    }
                }
                _zip.Close();
                tempZip.Close();

                File.Delete(_zip.FileName);
                File.Move(tempZipName, _zip.FileName);

                _zip = FwkZip.Open(_zip.FileName, _zip.Access);
            }
            catch
            {
                return(false);
            }
            finally
            {
                if (File.Exists(tempZipName))
                {
                    File.Delete(tempZipName);
                }
                if (File.Exists(tempEntryName))
                {
                    File.Delete(tempEntryName);
                }
            }
            return(true);
        }
示例#2
0
        /// <summary>
        /// Genera un archivo zip de todoslos archivos pasados por parametro
        /// para abrirlo en memoria se puede usar Byte[] zipFile = FileFunctions.OpenBinaryFile(zipFullName);
        /// </summary>
        /// <param name="selectedFilesToZip">Lista de archivos a comprimir</param>
        /// <param name="zipFullName">Nombre del archivo zip</param>
        /// <returns></returns>
        public static FileInfo Compress(String[] selectedFilesToZip, string zipFullName)
        {
            //string tempFolder = System.IO.Path.Combine(Environment.CurrentDirectory, "temp");
            //if (Directory.Exists(tempFolder))
            //    DeleteDirectory(tempFolder);

            //Directory.CreateDirectory(tempFolder);
            //copy selected files to temp folder
            //foreach (string filename in selectedFilesToZip)
            //{
            //    FileInfo f = new FileInfo(filename);
            //    f.CopyTo(tempFolder + @"\" + f.Name);
            //}
            //string zipFullName = System.IO.Path.Combine(tempFolder, zipFileName);



            FwkZip zip = FwkZip.Create(zipFullName, "Generated pelsoft");

            // Stores all the files into the zip file
            foreach (string file in selectedFilesToZip)
            {
                zip.AddFile(FwkZip.Compression.Deflate, file, Path.GetFileName(file), "");
            }

            //crear binario con el zip
            using (MemoryStream readme = new MemoryStream(
                       System.Text.Encoding.UTF8.GetBytes(string.Format("{0}\r\nThis file has been {1} using the ZipStorer class, by Jaime Olivares.",
                                                                        DateTime.Now, "created"))))
            {
                // Stores a new file directly from the stream
                zip.AddStream(FwkZip.Compression.Store, "readme.txt", readme, DateTime.Now, "Please read");
            }
            zip.Close();
            FileInfo f = new FileInfo(zipFullName);

            return(f);

            //Byte[] zipFile = FileFunctions.OpenBinaryFile(zipFullName);

            //return zipFile;
        }