GetEnumerator() public method

Gets an enumerator for the Zip entries in this Zip file.
/// The Zip file has been closed. ///
public GetEnumerator ( ) : IEnumerator
return IEnumerator
Example #1
0
        public Model Load(string fileName)
        {
            var document = new XmlDocument();

            if (fileName.EndsWith(".tcn"))
            {
                var file = new ZipFile(new FileStream(fileName, FileMode.Open, FileAccess.Read));

                var enumerator = file.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    if (((ZipEntry)enumerator.Current).Name.EndsWith(".xml"))
                    {
                        document.Load(file.GetInputStream((ZipEntry)enumerator.Current));
                        break;
                    }
                }
            }
            else if (fileName.EndsWith(".xml"))
                document.Load(fileName);
            else
                throw new FileLoadException();

            var tcnModel = new TCNFile();
            tcnModel.Parse(document.DocumentElement);

            return tcnModel.Convert();
        }
		/// <summary>
		/// Extracts the specified zip file stream.
		/// </summary>
		/// <param name="fileStream">The zip file stream.</param>
		/// <returns></returns>
		public void Extract(Stream fileStream)
		{
			_isValid = true;

			ZipFile zipFile = null;

			try
			{
				zipFile = new ZipFile(fileStream);

				IEnumerator enumerator = zipFile.GetEnumerator();

				while (enumerator.MoveNext())
				{
					ZipEntry entry = (ZipEntry)enumerator.Current;

					ExtractZipEntry(zipFile, entry);
				}
			}
			catch (Exception ex)
			{
				_isValid = false;
				_exceptionMessage = ex.Message;
			}
			finally
			{
				fileStream.Close();

				if (null != zipFile) zipFile.Close();
			}
		}
Example #3
0
        //jordenwu 精简
        public int UnityExtractStreamZipInit(Stream inputStream, string targetDirectory)
        {
            continueRunning_          = true;
            overwrite_                = Overwrite.Always;
            confirmDelegate_          = null;
            extractNameTransform_     = new WindowsNameTransform(targetDirectory);
            restoreDateTimeOnExtract_ = false;
            createEmptyDirectories_   = true;
            //
            zipFile_ = new ZipFile(inputStream);
            zipFile_.IsStreamOwner = false;
#if !NETCF_1_0
            if (password_ != null)
            {
                zipFile_.Password = password_;
            }
#endif
            //获取文件总数
            System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
            int fileCnt = 0;
            while (enumerator.MoveNext())
            {
                ZipEntry ze = enumerator.Current as ZipEntry;
                if (ze != null)
                {
                    if (ze.IsFile)
                    {
                        fileCnt++;
                    }
                }
            }
            return(fileCnt);
        }
Example #4
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param>
        public void ExtractZip(string zipFileName, string targetDirectory, string fileFilter, string directoryFilter)
        {
            continueRunning_      = true;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            fileFilter_      = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);

            using (zipFile_ = new ZipFile(zipFileName))
            {
                IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext())
                {
                    var entry = (ZipEntry)enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) &&
                            fileFilter_.IsMatch(entry.Name))
                        {
                            ExtractEntry(entry);
                        }
                    }
                }
            }
        }
Example #5
0
        public static bool unzipFile(Stream inputStream, string targetDirectory, bool overwrite, OnUnzipProgress callback)
        {
            bool ret = false;

            callback(0, 0);
            using (ZipFile zipFile_ = new ZipFile(inputStream))
            {
                int       totalBytes = (int)zipFile_.unzipSize;
                UnzipCach cach       = new UnzipCach();
                cach.start(totalBytes, callback, overwrite);

                INameTransform extractNameTransform_      = new WindowsNameTransform(targetDirectory);
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    try
                    {
                        ZipEntry entry = (ZipEntry)enumerator.Current;
                        if (entry.IsFile)
                        {
                            string fileName = extractNameTransform_.TransformFile(entry.Name);
                            string dirName  = Path.GetDirectoryName(Path.GetFullPath(fileName));
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                            Stream source = zipFile_.GetInputStream(entry);
                            cach.addFile(fileName, (int)entry.Size, source);
                            source.Close();
                        }
                        else
                        {
                            string dirName = extractNameTransform_.TransformDirectory(entry.Name);
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        cach.setState(UnzipCach.State.Error);
                        //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES);
                    }

                    if (cach.isError())
                    {
                        break;
                    }
                }

                cach.setState(UnzipCach.State.Ok);
                ret = cach.stop();
                callback(1, ret?0:1);
                return(ret);
            }
        }
Example #6
0
        /// <summary>
        /// Extract the contents of a zip file held in a stream.
        /// </summary>
        /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param>
        /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param>
        public void ExtractZip(Stream inputStream, string targetDirectory,
                               Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
                               string fileFilter, string directoryFilter, bool restoreDateTime,
                               bool isStreamOwner)
        {
            if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null))
            {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_      = true;
            overwrite_            = overwrite;
            confirmDelegate_      = confirmDelegate;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            fileFilter_               = new NameFilter(fileFilter);
            directoryFilter_          = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using (zipFile_ = new ZipFile(inputStream))
            {
#if !NETCF_1_0
                if (password_ != null)
                {
                    zipFile_.Password = password_;
                }
#endif
                zipFile_.IsStreamOwner = isStreamOwner;
                IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext())
                {
                    var entry = (ZipEntry)enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) &&
                            fileFilter_.IsMatch(entry.Name))
                        {
                            ExtractEntry(entry);
                        }
                    }
                    else if (entry.IsDirectory)
                    {
                        if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories)
                        {
                            ExtractEntry(entry);
                        }
                    }
                    else
                    {
                        // Do nothing for volume labels etc...
                    }
                }
            }
        }
Example #7
0
        void ExtractZipThread()
        {
            continueRunning_      = true;
            overwrite_            = Overwrite.Always;
            confirmDelegate_      = null;
            extractNameTransform_ = new WindowsNameTransform(mTmpDirectory);

            fileFilter_               = new NameFilter(null);
            directoryFilter_          = new NameFilter(null);
            restoreDateTimeOnExtract_ = false;

            using (zipFile_ = new ZipFile(mInputStream)) {
                                #if !NETCF_1_0
                if (password_ != null)
                {
                    zipFile_.Password = password_;
                }
                                #endif
                mUnzipSize             = (int)zipFile_.unzipSize;
                zipFile_.IsStreamOwner = true;
                WaitForEndOfFrame w = new WaitForEndOfFrame();
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext())
                {
                    ZipEntry entry = (ZipEntry)enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name))
                        {
                            ExtractEntry(entry);
                        }
                    }
                    else if (entry.IsDirectory)
                    {
                        if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories)
                        {
                            ExtractEntry(entry);
                        }
                    }
                    else
                    {
                        // Do nothing for volume labels etc...
                    }
                }
                mInputStream.Close();
                if (mUnzipSize == mUnzipBytes)
                {
                    Directory.Move(mTmpDirectory, mOutDirectory);
                    mCallback(1f, 0);
                }
            }
        }
Example #8
0
File: Zip.cs Project: nomit007/f4
 public static Map contents(ZipFile zipFile)
 {
     Map c = new Map(Sys.UriType, Sys.FileType);
       IEnumerator e = zipFile.GetEnumerator();
       while (e.MoveNext())
       {
     ZipEntry entry = (ZipEntry)e.Current;
     ZipEntryFile f = new ZipEntryFile(zipFile, entry);
     c.set(f.m_uri, f);
       }
       return c;
 }
Example #9
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param>
        public void ExtractZip(string zipFileName, string targetDirectory,
                               Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
                               string fileFilter, string directoryFilter, bool restoreDateTime)
        {
            if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null))
            {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_          = true;
            overwrite_                = overwrite;
            confirmDelegate_          = confirmDelegate;
            targetDirectory_          = targetDirectory;
            fileFilter_               = new NameFilter(fileFilter);
            directoryFilter_          = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using (zipFile_ = new ZipFile(zipFileName)) {
#if !NETCF_1_0
                if (password_ != null)
                {
                    zipFile_.Password = password_;
                }
#endif

                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext())
                {
                    ZipEntry entry = (ZipEntry)enumerator.Current;
                    if (entry.IsFile)
                    {
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name))
                        {
                            ExtractEntry(entry);
                        }
                    }
                    else if (entry.IsDirectory)
                    {
                        if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories)
                        {
                            ExtractEntry(entry);
                        }
                    }
                    else
                    {
                        // Do nothing for volume labels etc...
                    }
                }
            }
        }
Example #10
0
        /// <summary>
        /// Extract ZIP to memory
        /// </summary>
        /// <param name="inputStream"></param>
        public List<ZipEntry> ExtractZip(Stream inputStream)
        {
            using (ZipFile zip_file = new ZipFile(inputStream)) {
                System.Collections.IEnumerator enumerator = zip_file.GetEnumerator();
                while ( enumerator.MoveNext()) {
                    ZipEntry entry = (ZipEntry)enumerator.Current;

                    if (entry.IsFile)
                    {
                        ExtractEntry(entry);
                    //	ZipOutputStream zos = ((ZipFile)entry).GetOutputStream( entry );
                    }
                    else if (entry.IsDirectory) {
                        ExtractEntry(entry);
                    }
                }
            }
        }
Example #11
0
        /// <summary>
        /// Extract the contents of a zip file held in a stream.
        /// </summary>
        /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param>
        /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param>
        public void ExtractZip(Stream inputStream, string targetDirectory,
                               Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
                               string fileFilter, string directoryFilter, bool restoreDateTime,
                               bool isStreamOwner)
        {
            if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null))
            {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_      = true;
            overwrite_            = overwrite;
            confirmDelegate_      = confirmDelegate;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            fileFilter_               = new NameFilter(fileFilter);
            directoryFilter_          = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using (zipFile_ = new ZipFile(inputStream))
            {
                                #if !NETCF_1_0
                if (password_ != null)
                {
                    zipFile_.Password = password_;
                }
                                #endif
                zipFile_.IsStreamOwner = isStreamOwner;
                extractEnum_           = zipFile_.GetEnumerator();
                totalCount_            = 0;
                while (continueRunning_ && extractEnum_.MoveNext())
                {
                    ZipEntry entry = (ZipEntry)extractEnum_.Current;
                    totalCount_ += entry.Size;
                }
                extractEnum_.Reset();

                doneCount_ = 0;
                ExtractUpdate();
            }
        }
Example #12
0
 public void ExtractZip(Stream inputStream, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime, bool isStreamOwner)
 {
     //IL_000c: Unknown result type (might be due to invalid IL or missing references)
     if (overwrite == Overwrite.Prompt && confirmDelegate == null)
     {
         throw new ArgumentNullException("confirmDelegate");
     }
     continueRunning_          = true;
     overwrite_                = overwrite;
     confirmDelegate_          = confirmDelegate;
     extractNameTransform_     = new WindowsNameTransform(targetDirectory);
     fileFilter_               = new NameFilter(fileFilter);
     directoryFilter_          = new NameFilter(directoryFilter);
     restoreDateTimeOnExtract_ = restoreDateTime;
     using (zipFile_ = new ZipFile(inputStream))
     {
         if (password_ != null)
         {
             zipFile_.Password = password_;
         }
         zipFile_.IsStreamOwner = isStreamOwner;
         global::System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
         while (continueRunning_ && enumerator.MoveNext())
         {
             ZipEntry zipEntry = (ZipEntry)enumerator.get_Current();
             if (zipEntry.IsFile)
             {
                 if (directoryFilter_.IsMatch(Path.GetDirectoryName(zipEntry.Name)) && fileFilter_.IsMatch(zipEntry.Name))
                 {
                     ExtractEntry(zipEntry);
                 }
             }
             else if (zipEntry.IsDirectory && directoryFilter_.IsMatch(zipEntry.Name) && CreateEmptyDirectories)
             {
                 ExtractEntry(zipEntry);
             }
         }
     }
 }
Example #13
0
        public bool BeginExtractZipSelf(string orgPath, string tarPath, string filefilter)
        {
            bool res = true;

            try
            {
                Stream inputStream = File.Open(orgPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                continueRunning_      = true;
                overwrite_            = Overwrite.Always;
                confirmDelegate_      = null;
                extractNameTransform_ = new WindowsNameTransform(tarPath);

                fileFilter_      = new NameFilter(filefilter);
                directoryFilter_ = new NameFilter(null);
                //restoreDateTimeOnExtract_ = restoreDateTimeOnExtract_;

                zipFile_ = new ZipFile(inputStream);

                {
#if !NETCF_1_0
                    if (password_ != null)
                    {
                        zipFile_.Password = password_;
                    }
#endif
                    zipFile_.IsStreamOwner = true;
                    enumerator_            = zipFile_.GetEnumerator();
                }
            }
            catch
            {
                res = false;
            }

            return(res);
        }
Example #14
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param>
        public void ExtractZip(string zipFileName, string targetDirectory, 
							   Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, 
							   string fileFilter, string directoryFilter, bool restoreDateTime)
        {
            if ( (overwrite == Overwrite.Prompt) && (confirmDelegate == null) ) {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_ = true;
            overwrite_ = overwrite;
            confirmDelegate_ = confirmDelegate;
            targetDirectory_ = targetDirectory;
            fileFilter_ = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using ( zipFile_ = new ZipFile(zipFileName) ) {

            #if !NETCF_1_0
                if (password_ != null) {
                    zipFile_.Password = password_;
                }
            #endif

                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while ( continueRunning_ && enumerator.MoveNext()) {
                    ZipEntry entry = (ZipEntry) enumerator.Current;
                    if ( entry.IsFile )
                    {
                        if ( directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name) ) {
                            ExtractEntry(entry);
                        }
                    }
                    else if ( entry.IsDirectory ) {
                        if ( directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories ) {
                            ExtractEntry(entry);
                        }
                    }
                    else {
                        // Do nothing for volume labels etc...
                    }
                }
            }
        }
Example #15
0
        /// <summary>
        /// Extract the contents of a zip file held in a stream.
        /// </summary>
        /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param>
        /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param>
        public void ExtractZip(Stream inputStream, string targetDirectory,
                       Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
                       string fileFilter, string directoryFilter, bool restoreDateTime,
                       bool isStreamOwner)
        {
            if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_ = true;
            overwrite_ = overwrite;
            confirmDelegate_ = confirmDelegate;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            fileFilter_ = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);
            restoreDateTimeOnExtract_ = restoreDateTime;

            using (zipFile_ = new ZipFile(inputStream)) {

#if !NETCF_1_0
                if (password_ != null) {
                    zipFile_.Password = password_;
                }
#endif
                zipFile_.IsStreamOwner = isStreamOwner;
                IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext()) {
                    ZipEntry entry = (ZipEntry)enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) {
                            ExtractEntry(entry);
                        }
                    }
                    else if (entry.IsDirectory) {
                        if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) {
                            ExtractEntry(entry);
                        }
                    }
                    else {
                        // Do nothing for volume labels etc...
                    }
                }
            }
        }
Example #16
0
        public static long GetUnzipFileSize(string strZipFile)
        {
            long lngUnzipFileSize = 0;

            try
            {
                ZipFile zf = new ZipFile(strZipFile);

                System.Collections.IEnumerator ie = zf.GetEnumerator();

                while (ie.MoveNext())
                {
                    ZipEntry ze = (ZipEntry)ie.Current;
                    lngUnzipFileSize += ze.Size;
                }

                zf.Close();

                return lngUnzipFileSize;
            }
            catch (Exception ex)
            {
                //LogClass.WriteLog("GetUnzipFileSize Exception: " + ex.Message);
                lngUnzipFileSize = 0;
                return lngUnzipFileSize;
            }
        }
Example #17
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A filter to apply to files.</param>
        /// <param name="directoryFilter">A filter to apply to directories.</param>
        /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param>
        public void ExtractZip(string zipFileName, string targetDirectory, string fileFilter, string directoryFilter)
        {
            continueRunning_ = true;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            fileFilter_ = new NameFilter(fileFilter);
            directoryFilter_ = new NameFilter(directoryFilter);

            using (zipFile_ = new ZipFile(zipFileName))
            {
                IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext())
                {
                    var entry = (ZipEntry) enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) &&
                            fileFilter_.IsMatch(entry.Name))
                        {
                            ExtractEntry(entry);
                        }
                    }
                }
            }
        }
Example #18
0
		/// <summary>
		/// Extracts the specified zip file stream.
		/// </summary>
		/// <param name="fileStream">The zip file stream.</param>
		/// <returns></returns>
		public bool Extract(Stream fileStream)
		{
			if (null == fileStream) return false;

			CleanFromTemp(false);

			NewTempPath();

			_isValid = true;

			ZipFile zipFile = null;

			try
			{
				zipFile = new ZipFile(fileStream);

				IEnumerator enumerator = zipFile.GetEnumerator();

				while (enumerator.MoveNext())
				{
					ZipEntry entry = (ZipEntry)enumerator.Current;

					ExtractZipEntry(zipFile, entry);
				}
			}
			catch (Exception ex)
			{
				_isValid = false;
				_exceptionMessage = ex.Message;

				CleanFromTemp(true); //true tells CleanFromTemp not to raise an IO Exception if this operation fails. If it did then the real error here would be masked
				
			}
			finally
			{
				fileStream.Close();

				if (null != zipFile) zipFile.Close();
			}

			return _isValid ? CheckFolderTree() : false;
		}
Example #19
0
        static void unzipFileThread()
        {
            //LogManager.GetInstance().LogMessage("unzip begin outpath="+_targetDirectory, LogManager.ModuleFilter.RES);
            _callback(0, 0);
            bool ret = false;

            using (ZipFile zipFile_ = new ZipFile(_inputStream))
            {
                int       totalBytes = (int)zipFile_.unzipSize;
                UnzipCach cach       = new UnzipCach();
                cach.start(totalBytes, _callback);

                INameTransform extractNameTransform_      = new WindowsNameTransform(_tmpDirectory);
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    try
                    {
                        ZipEntry entry = (ZipEntry)enumerator.Current;
                        if (entry.IsFile)
                        {
                            string fileName = extractNameTransform_.TransformFile(entry.Name);
                            string dirName  = Path.GetDirectoryName(Path.GetFullPath(fileName));
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                            Stream source = zipFile_.GetInputStream(entry);
                            cach.addFile(fileName, (int)entry.Size, source);
                            source.Close();
                        }
                        else
                        {
                            string dirName = extractNameTransform_.TransformDirectory(entry.Name);
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        cach.setState(UnzipCach.State.Error);
                        //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES);
                    }

                    if (cach.isError())
                    {
                        break;
                    }
                }

                cach.setState(UnzipCach.State.Ok);
                if (cach.stop())
                {
                    try
                    {
                        Directory.Move(_tmpDirectory, _targetDirectory);
                        ret = true;
                    }
                    catch (Exception e)
                    {
                        //LogManager.GetInstance().LogException("unzip rename dir error.", e);
                    }
                }
                _callback(1, ret?0:1);
                //LogManager.GetInstance().LogMessage("unzip end", LogManager.ModuleFilter.RES);
                _inputStream = null;
                _callback    = null;
            }
        }
Example #20
0
 public static List<DEngine> Load(string fileName, UndoRedoArea undoRedoArea, out DPoint pageSize, out BackgroundFigure bf, string[] extraEntryDirs, out Dictionary<string, byte[]> extraEntries)
 {
     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
     pageSize = null;
     bf = null;
     extraEntries = null;
     List<DEngine> res = new List<DEngine>();
     // load zipfile
     using (ZipFile zf = new ZipFile(fileName))
     {
         // find pages ini file entry
         byte[] data = Read(zf, PAGES_INI);
         if (data != null)
         {
             // read general background figure
             byte[] genBkgndFigureData = Read(zf, GENBKGNDFIGURE);
             if (genBkgndFigureData != null)
             {
                 List<Figure> figs = FigureSerialize.FromXml(encoding.GetString(genBkgndFigureData));
                 if (figs.Count == 1 && figs[0] is BackgroundFigure)
                 {
                     LoadImage(zf, figs[0]);
                     bf = (BackgroundFigure)figs[0];
                     // read page size from background figure
                     if (bf.Width > 0 && bf.Height > 0)
                         pageSize = new DPoint(bf.Width, bf.Height);
                 }
             }
             // create Nini config source from pages ini entry stream
             IniConfigSource source = new IniConfigSource(new MemoryStream(data));
             // load each page info mentioned in ini entry
             foreach (IConfig config in source.Configs)
             {
                 // create new DEngine for page
                 DEngine de = new DEngine(undoRedoArea);
                 // set page size
                 if (config.Contains(PAGESIZE))
                     de.PageSize = DPoint.FromString(config.Get(PAGESIZE));
                 if (config.Contains(PAGENAME))
                     de.PageName = config.Get(PAGENAME);
                 // set the figures
                 if (config.Contains(FIGURELIST))
                 {
                     string figureListEntryName = config.Get(FIGURELIST);
                     data = Read(zf, figureListEntryName);
                     if (data != null)
                     {
                         List<Figure> figs = FigureSerialize.FromXml(encoding.GetString(data));
                         foreach (Figure f in figs)
                         {
                             de.AddFigure(f);
                             LoadImage(zf, f);
                         }
                     }
                 }
                 // set the background figure
                 if (config.Contains(BACKGROUNDFIGURE))
                 {
                     string backgroundFigureEntryName = config.Get(BACKGROUNDFIGURE);
                     data = Read(zf, backgroundFigureEntryName);
                     if (data != null)
                     {
                         List<Figure> figs = FigureSerialize.FromXml(encoding.GetString(data));
                         if (figs.Count == 1 && figs[0] is BackgroundFigure)
                         {
                             LoadImage(zf, figs[0]);
                             de.SetBackgroundFigure((BackgroundFigure)figs[0], true);
                         }
                     }
                 }
                 else if (bf != null)
                     de.SetBackgroundFigure(bf, false);
                 // add to list of DEngines
                 res.Add(de);
             }
             // read extra entries
             if (extraEntryDirs != null)
             {
                 extraEntries = new Dictionary<string, byte[]>();
                 foreach (string dir in extraEntryDirs)
                 {
                     IEnumerator en = zf.GetEnumerator();
                     en.Reset();
                     while (en.MoveNext())
                     {
                         ZipEntry entry = (ZipEntry)en.Current;
                         if (entry.Name.IndexOf(dir) == 0)
                             extraEntries.Add(entry.Name, Read(zf, entry.Name));
                     }
                 }
             }
         }
     }
     return res;
 }
Example #21
0
        /// <summary>
        /// Extract the contents of a zip file.
        /// </summary>
        /// <param name="zipFileName">The zip file to extract from.</param>
        /// <param name="targetDirectory">The directory to save extracted information in.</param>
        /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
        /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
        /// <param name="fileFilter">A method-filter to apply to files.</param>
        /// <param name="directoryFilter">A method-filter to apply to directories.</param>
        public void ExtractZip(
            string zipFileName,
            string targetDirectory,
            Overwrite overwrite,
            ConfirmOverwriteDelegate confirmDelegate,
            Func<string, bool> fileFilter,
            Func<string, bool> directoryFilter)
        {
            if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null))
            {
                throw new ArgumentNullException("confirmDelegate");
            }

            continueRunning_ = true;
            overwrite_ = overwrite;
            confirmDelegate_ = confirmDelegate;
            extractNameTransform_ = new WindowsNameTransform(targetDirectory);

            restoreDateTimeOnExtract_ = true;

            Stream inputStream = File.Open(zipFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            using (zipFile_ = new ZipFile(inputStream))
            {

            #if !NETCF_1_0
                if (password_ != null)
                {
                    zipFile_.Password = password_;
                }
            #endif
                zipFile_.IsStreamOwner = true;
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (continueRunning_ && enumerator.MoveNext())
                {
                    ZipEntry entry = (ZipEntry)enumerator.Current;
                    if (entry.IsFile)
                    {
                        // TODO Path.GetDirectory can fail here on invalid characters.
                        if (directoryFilter(Path.GetDirectoryName(entry.Name)) && fileFilter(entry.Name))
                        {
                            ExtractEntry(entry);
                        }
                    }
                    else if (entry.IsDirectory)
                    {
                        if (directoryFilter(entry.Name) && CreateEmptyDirectories)
                        {
                            ExtractEntry(entry);
                        }
                    }
                }
            }
        }
Example #22
0
        /// <summary>
        /// Extracts the specified zip file stream.
        /// </summary>
        /// <param name="fileStream">The zip file stream.</param>
        /// <returns></returns>
        public bool Extract(Stream fileStream)
        {
            if (null == fileStream) return false;

            CleanFromTemp();

            NewTempPath();

            _isValid = true;

            ZipFile zipFile = null;

            try
            {
                zipFile = new ZipFile(fileStream);

                IEnumerator enumerator = zipFile.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    ZipEntry entry = (ZipEntry)enumerator.Current;

                    ExtractZipEntry(zipFile, entry);
                }
            }
            catch (Exception ex)
            {
                _isValid = false;
                _exceptionMessage = ex.Message;

                CleanFromTemp();
            }
            finally
            {
                fileStream.Close();

                if (null != zipFile) zipFile.Close();
            }

            return _isValid ? CheckFolderTree() : false;
        }
Example #23
0
        /// <summary>
        /// Extracts the specified zip file stream.
        /// </summary>
        /// <param name="fileStream">The zip file stream.</param>
        /// <returns></returns>
        public bool Extract(Stream fileStream)
        {
            if (null == fileStream) return false;

            CleanFromTemp();

            NewTempPath();

            _isValid = true;

            ZipFile zipFile = null;

            try
            {
                zipFile = new ZipFile(fileStream);
                
                IEnumerator enumerator = zipFile.GetEnumerator();
                //System.Diagnostics.Debug.WriteLine(string.Format("Enumerating {0} zipfiles", zipFile.Count.ToString()));
                while (enumerator.MoveNext())
                {
                    ZipEntry entry = (ZipEntry)enumerator.Current;
                  //  System.Diagnostics.Debug.WriteLine(entry.ZipFileIndex.ToString() + " " + entry.Name);
                    
                    ExtractZipEntry(zipFile, entry);
                }
            }
            catch (Exception ex)
            {
                _isValid = false;
                _exceptionMessage = ex.Message;

                CleanFromTemp();
            }
            finally
            {
                fileStream.Close();

                if (null != zipFile) zipFile.Close();
            }

            return _isValid ? CheckFolderTree() : false;
        }
Example #24
0
    public ExcelDocument(string filename)
    {
        if (!filename.Contains("\\"))
        {
            filename = FileService.GetPath() + "\\" + filename;
        }
        WorkSheets = new ObservableCollection <WorkSheet>();
        try
        {
            using (zip.ZipFile zf = new zip.ZipFile(filename))
            {
                IEnumerator ienum = zf.GetEnumerator();
                int         ifld  = zf.FindEntry("xl/workbook.xml", true);

                StreamReader srwb = new StreamReader(zf.GetInputStream(ifld));

                XmlDocument xd = new XmlDocument();
                xd.LoadXml(srwb.ReadToEnd());
                foreach (XmlNode xn in xd.DocumentElement.GetElementsByTagName("sheet"))
                {
                    string sheetId = xn.Attributes["sheetId"].Value;
                    string name    = xn.Attributes["name"].Value;
                    int    intsh   = zf.FindEntry("xl/worksheets/sheet" + sheetId + ".xml", true);
                    if (intsh != -1)
                    {
                        StreamReader sr = new StreamReader(zf.GetInputStream(intsh));
                        WorkSheet    ws = new WorkSheet()
                        {
                            SheetName = name, SheetXml = sr.ReadToEnd()
                        };

                        XmlDocument xdr = new XmlDocument();
                        xdr.LoadXml(ws.SheetXml);
                        string decsep    = (1.5).ToString();
                        string repdecsep = decsep.Contains(",") ? "," : decsep.Contains(".") ? "." : "";
                        decsep = decsep.Contains(",") ? "." : decsep.Contains(".") ? "," : "";

                        foreach (XmlNode xnr in xdr.DocumentElement.GetElementsByTagName("row"))
                        {
                            try
                            {
                                ExcelRow row = new ExcelRow();
                                row.Row  = int.Parse(xnr.Attributes["r"].Value.ToString());
                                row.Span = xnr.Attributes["spans"].Value;
                                foreach (XmlNode xnc in xnr.ChildNodes)
                                {
                                    try
                                    {
                                        ExcelCell cell = new ExcelCell();
                                        cell.Coord   = xnc.Attributes["r"].Value;
                                        cell.Formula = xnc.ChildNodes.Count > 0 && xnc.FirstChild != null && xnc.FirstChild.Name.Equals("f") ? xnc.FirstChild.InnerText : "";
                                        cell.Value   = xnc.ChildNodes.Count == 1 && xnc.FirstChild != null && xnc.FirstChild.Name.Equals("v") && !string.IsNullOrEmpty(xnc.FirstChild.InnerText) ? xnc.FirstChild.InnerText :
                                                       xnc.ChildNodes.Count > 1 && xnc.LastChild != null && xnc.LastChild.Name.Equals("v") && !string.IsNullOrEmpty(xnc.LastChild.InnerText) ? xnc.LastChild.InnerText : "0";
                                        row.Cells.Add(cell);
                                        row.setA2Z(cell.Coord);
                                    }
                                    catch (Exception ex) { string ss = ex.Message; }
                                }
                                ws.Rows.Add(row);
                            }
                            catch (Exception ex) { string ss = ex.Message; }
                        }

                        WorkSheets.Add(ws);
                    }
                }
            }
        }
        catch (Exception ex) { string ss = ex.Message; }
    }
 public ZipEntryWrapperEnumerator(ZipFile file)
 {
   enumerator = file.GetEnumerator();
 }