示例#1
0
        /// <summary>
        /// Method to open an existing storage file
        /// </summary>
        /// <param name="_filename">Full path of Zip file to open</param>
        /// <param name="_access">File access mode as used in FileStream constructor</param>
        /// <returns>A valid FwkZip object</returns>
        static FwkZip Open(string _filename, FileAccess _access)
        {
            Stream stream = (Stream) new FileStream(_filename, FileMode.Open, _access == FileAccess.Read ? FileAccess.Read : FileAccess.ReadWrite);

            FwkZip zip = Open(stream, _access);

            zip.FileName = _filename;

            return(zip);
        }
示例#2
0
        /// <summary>
        /// Method to create a new zip storage in a stream
        /// </summary>
        /// <param name="_stream"></param>
        /// <param name="_comment"></param>
        /// <returns>A valid FwkZip object</returns>
        static FwkZip Create(Stream _stream, string _comment)
        {
            FwkZip zip = new FwkZip();

            zip.Comment       = _comment;
            zip.ZipFileStream = _stream;
            zip.Access        = FileAccess.Write;

            return(zip);
        }
示例#3
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);
        }
示例#4
0
        /// <summary>
        /// Method to create a new storage file
        /// </summary>
        /// <param name="_filename">Full path of Zip file to create</param>
        /// <param name="_comment">General comment for Zip file</param>
        /// <returns>A valid FwkZip object</returns>
        public static FwkZip Create(string _filename, string _comment)
        {
            Stream stream = new FileStream(_filename, FileMode.Create, FileAccess.ReadWrite);

            FwkZip zip = Create(stream, _comment);

            zip.Comment  = _comment;
            zip.FileName = _filename;

            return(zip);
        }
示例#5
0
        /// <summary>
        /// Crea el archivo Zip en la ruta extractPath
        /// </summary>
        /// <param name="zipFileFullName">Nombre comppleto del archivo zip</param>
        /// <param name="extractPath">ruta destino de la extracción</param>
        public static void Extract(String zipFileFullName, string extractPath)
        {
            ////buscar el archivo zip
            //StoderFiles file = ChekUpdaterDAC.Get_Zip(lastVersion, appGuid);

            ////Obtener nombre sin .zip
            //String zipFileName = file.Title.Substring(0, file.Title.Length - 4);
            //zipFileName = zipFileName + "_" + HelpersFunctions.ToDDMMYYYY(DateTime.Now, '-') + ".zip";
            //Guardo
            //HelpersFunctions.SaveBinaryFile(zipFileName, file.ZipFile);


            StringBuilder strRes = new StringBuilder();

            FileInfo zipFileInfo = new FileInfo(zipFileFullName);
            //zipFileName = zipFileInfo.FullName;
            // extractPath = Path.Combine(Environment.CurrentDirectory, "temp");

            //if (System.IO.Directory.Exists(extractPath) == false)
            //    System.IO.Directory.CreateDirectory(extractPath);


            // Opens existing zip file
            FwkZip zip = FwkZip.Open(zipFileFullName, FileAccess.Read);
            // Read all directory contents
            List <FwkZip.ZipFileEntry> dir = zip.ReadCentralDir();

            bool   result;
            string path;

            try
            {
                foreach (FwkZip.ZipFileEntry entry in dir)
                {
                    path   = Path.Combine(extractPath, Path.GetFileName(entry.FilenameInZip));
                    result = zip.ExtractFile(entry, path);
                    strRes.AppendLine(path + (result ? "" : " (error)"));
                }
                zip.Close();
            }
            catch (Exception ex)
            {
                zip.Close();

                throw ex;
            }
        }
示例#6
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;
        }
示例#7
0
        /// <summary>
        /// Method to open an existing storage from stream
        /// </summary>
        /// <param name="_stream">Already opened stream with zip contents</param>
        /// <param name="_access">File access mode for stream operations</param>
        /// <returns>A valid FwkZip object</returns>
        static FwkZip Open(Stream _stream, FileAccess _access)
        {
            if (!_stream.CanSeek && _access != FileAccess.Read)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            FwkZip zip = new FwkZip();

            //zip.FileName = _filename;
            zip.ZipFileStream = _stream;
            zip.Access        = _access;

            if (zip.ReadFileInfo())
            {
                return(zip);
            }

            throw new System.IO.InvalidDataException();
        }