示例#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>
        /// 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;
            }
        }