示例#1
0
        public Entry CreateEntry(string dir, string name)
        {
            ExceptionContext.Check(!Disposed);

            Flush();

            string pathEnt;
            string pathTemp;

            GetPath(out pathEnt, out pathTemp, dir, name, true);
            if (!PathMap.TryAdd(pathEnt, pathTemp))
            {
                throw ExceptionContext.ExceptParam(nameof(name), "Duplicate entry: '{0}'", pathEnt);
            }

            Stream stream;

            if (pathTemp != null)
            {
                stream = new FileStream(pathTemp, FileMode.CreateNew);
            }
            else
            {
                stream = new MemoryStream();
            }

            return(AddEntry(pathEnt, stream));
        }
示例#2
0
        public Entry OpenEntryOrNull(string dir, string name)
        {
            ExceptionContext.Check(!Disposed);

            string pathEnt;
            string pathTemp;

            GetPath(out pathEnt, out pathTemp, dir, name, false);

            ZipArchiveEntry entry;
            Stream          stream;
            string          pathAbs;
            string          pathLower = pathEnt.ToLowerInvariant();

            if (PathMap.TryGetValue(pathLower, out pathAbs))
            {
                stream = new FileStream(pathAbs, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            else
            {
                if (!_entries.TryGetValue(pathEnt, out entry))
                {
                    //Read old zip file that use backslash in filename
                    var pathEntTmp = pathEnt.Replace("/", "\\");
                    if (!_entries.TryGetValue(pathEntTmp, out entry))
                    {
                        return(null);
                    }
                }

                if (pathTemp != null)
                {
                    // Extract to a temporary file.
                    Directory.CreateDirectory(Path.GetDirectoryName(pathTemp));
                    entry.ExtractToFile(pathTemp);
                    if (!PathMap.TryAdd(pathLower, pathTemp))
                    {
                        throw ExceptionContext.ExceptParam(nameof(name), "Duplicate entry: '{0}'", pathLower);
                    }

                    stream = new FileStream(pathTemp, FileMode.Open, FileAccess.Read, FileShare.Read);
                }
                else
                {
                    // Extract to a memory stream.
                    ExceptionContext.CheckDecode(entry.Length < int.MaxValue, "Repository stream too large to read into memory");
                    stream = new MemoryStream((int)entry.Length);
                    using (var src = entry.Open())
                        src.CopyTo(stream);
                    stream.Position = 0;
                }
            }

            return(AddEntry(pathEnt, stream));
        }