예제 #1
0
파일: Zipper.cs 프로젝트: dannisliang/VS
        private bool IsIncludeFile(string zippedName, bool isDir, FileSpecMatcher includes, FileSpecMatcher excludes)
        {
            if (includes == null || includes.MatchSpecs(zippedName, isDir))
            {
                if (excludes == null || !excludes.MatchSpecs(zippedName, isDir))
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
        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");
            }
        }
예제 #3
0
파일: Zipper.cs 프로젝트: dannisliang/VS
        public void Zip()
        {
            if (ZipFile == null)
            {
                throw new ArgumentException("ZipFile is null");
            }
            if (ItemList.Count == 0)
            {
                throw new ArgumentException("ItemList is empty");
            }

            if (Path.GetExtension(ZipFile).Length == 0)
            {
                ZipFile = Path.ChangeExtension(ZipFile, "zip");
            }

            string realZipFile = null;

            if (UseTempFile)
            {
                realZipFile = ZipFile;
                ZipFile     = GetTempFileName(ZipFile);
            }

            FileSpecMatcher excludes = null;

            if (ExcludeFollowing.Count > 0)
            {
                excludes = new FileSpecMatcher(ExcludeFollowing, true);
            }
            FileSpecMatcher includes = null;

            if (IncludeOnlyFollowing.Count > 0)
            {
                includes = new FileSpecMatcher(IncludeOnlyFollowing, true);
            }

            pBuffer = new byte[4096];


            /*
             * 1) collect files. if we find a file several times its ok, as long as the zipped name is the same, else exception! (typically when 2 items are same dir, but different level and we store relative path)
             * Same with zipped name: if two different files map to same zipped name -> exception (typically when no path is stored + recursive)
             *
             *
             */

            List <FileSystemEntry> fsEntries = CollectFileSystemEntries();

            try
            {
                bool addedSomeEntry = false;

                //hmmm...denne vil adde hvis fila eksisterer? Nei...vi bruker append = 0
                using (ZipWriter writer = new ZipWriter(ZipFile))
                {
                    writer.Comment = this.Comment;

                    foreach (FileSystemEntry fsEntry in fsEntries)
                    {
                        if (IsIncludeFile(fsEntry.ZippedName, fsEntry.IsDirectory, includes, excludes))
                        {
                            if (fsEntry.IsDirectory)
                            {
                                if (!AddDirEntries)
                                {
                                    throw new Exception("!AddDirEntries but still got dir");
                                }

                                DirectoryInfo di    = (DirectoryInfo)fsEntry.FileSystemInfo;
                                ZipEntry      entry = new ZipEntry(fsEntry.ZippedName, true);
                                entry.ModifiedTime   = GetLastWriteTimeFixed(di);
                                entry.FileAttributes = di.Attributes;
                                entry.UTF8Encoding   = this.UTF8Encoding;
                                entry.Zip64          = (this.Zip64 == enZip64.Yes);
                                entry.Method         = CompressionMethod.Stored;                         //DIR
                                writer.AddEntry(entry);
                            }
                            else
                            {
                                FileInfo fi = (FileInfo)fsEntry.FileSystemInfo;
                                if (this.Zip64 == enZip64.No && fi.Length > UInt32.MaxValue)
                                {
                                    throw new NotSupportedException("Files above 4GB only supported with Zip64 enabled or auto");
                                }
                                ZipEntry entry = new ZipEntry(fsEntry.ZippedName);
                                entry.ModifiedTime   = GetLastWriteTimeFixed(fi);
                                entry.FileAttributes = fi.Attributes;
                                entry.UTF8Encoding   = this.UTF8Encoding;
                                entry.Zip64          = (this.Zip64 == enZip64.Yes) || (fi.Length > UInt32.MaxValue && this.Zip64 == enZip64.Auto);
                                if (fi.Length == 0 || IsStoreFile(fsEntry.ZippedName))
                                {
                                    entry.Method = CompressionMethod.Stored;
                                }
                                writer.AddEntry(entry);

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

                            addedSomeEntry = true;
                        }
                    }
                }

                if (!addedSomeEntry)
                {
                    throw new ZipException("Nothing to add");
                }

                if (UseTempFile)
                {
                    File.Delete(realZipFile);                     //overwrite
                    File.Move(ZipFile, realZipFile);
                    ZipFile = realZipFile;
                }
            }
            catch
            {
                File.Delete(ZipFile);
                throw;
            }
            //finally
            //{
            //    File.Delete(tempZip);
            //}
        }