コード例 #1
0
 public ZipEntryEnumerator(ZipReader zr)
 {
     pReader = zr;
 }
コード例 #2
0
ファイル: UnZipper.cs プロジェクト: woxihuanjia/zlibnet
        public void UnZip()
        {
            if (Destination == null)
            {
                throw new ArgumentException("Destination is null");
            }
            if (ItemList.Count == 0)
            {
                throw new ArgumentException("ItemList is empty");
            }
            //if (Filespecs.Count == 0)
            //    Filespecs.Add("*");

            FileSpecMatcher fileSpecs = new FileSpecMatcher(ItemList, Recurse);

            bool unzippedSomeEntry = false;

            using (ZipReader reader = new ZipReader(ZipFile))
            {
                // buffer to hold temp bytes
                buffer = new byte[4096];

                foreach (ZipEntry entry in reader)
                {
                    if (fileSpecs.MatchSpecs(entry.Name, entry.IsDirectory))
                    {
                        if (entry.IsDirectory)
                        {
                            //FIXME: bør kanskje ha sjekk på om flere filer med samme navn havner på rota og overskriver hverandre?
                            if (!NoDirectoryNames)
                            {
                                string        dirName = CreateUnzippedName(entry);
                                DirectoryInfo di      = new DirectoryInfo(dirName);
                                if (!di.Exists)
                                {
                                    di.Create();
                                }
                                SetLastWriteTimeFixed(di, entry.ModifiedTime);
                            }
                        }
                        else
                        {
                            string   fileName = CreateUnzippedName(entry);
                            FileInfo fi       = new FileInfo(fileName);
                            if (!fi.Directory.Exists)
                            {
                                fi.Directory.Create();
                            }

                            if (fi.Exists)
                            {
                                switch (IfFileExist)
                                {
                                case enIfFileExist.Exception:
                                    throw new ZipException("File already exists: " + fileName);

                                case enIfFileExist.Skip:
                                    continue;

                                case enIfFileExist.Overwrite:
                                    break;                                             //fall thru

                                default:
                                    throw new NotImplementedException("enIfFileExist " + IfFileExist);
                                }
                            }

                            using (FileStream writer = fi.Create())
                            {
                                int byteCount;
                                while ((byteCount = reader.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    writer.Write(buffer, 0, byteCount);
                                }
                            }

                            SetLastWriteTimeFixed(fi, entry.ModifiedTime);
                        }

                        unzippedSomeEntry = true;
                    }
                }
            }

            if (!unzippedSomeEntry)
            {
                throw new ZipException("No files to unzip");
            }
        }