Ejemplo n.º 1
0
        internal ZlibZipReader(string path)
        {
            string resolvedPath = ZipLib.ResolvePath(path);

            if (!File.Exists(resolvedPath))
            {
                throw new FileNotFoundException("File does not exist:" + path);
            }



            this.handle = ZipLib.unzOpen(resolvedPath);
            if (handle == IntPtr.Zero)
            {
                throw new ZipException("Unable to open ZIP file:" + path);
            }
        }
Ejemplo n.º 2
0
        public override void AddEntry(string relativePath, CompressionMethod compressionMethod)
        {
            string           resolvedPath = ZipLib.ResolvePath(relativePath);
            ZipFileEntryInfo info;

            info.DateTime = DateTime.Now;

            if (this.entryOpened)
            {
                ZipLib.zipCloseFileInZip(this.handle);
                this.entryOpened = false;
            }

            int result = ZipLib.zipOpenNewFileInZip(this.handle, resolvedPath, out info, null, 0, null, 0, String.Empty, (int)compressionMethod, (int)CompressionLevel.Default);

            if (result < 0)
            {
                throw new ZipException("Error while opening entry for writing: " + relativePath + " - Errorcode: " + result);
            }
            this.entryOpened = true;
        }
Ejemplo n.º 3
0
        internal ZlibZipWriter(string path)
        {
            this.Filename = path;
            string resolvedPath = ZipLib.ResolvePath(path);

            this.handle = ZipLib.zipOpen(resolvedPath, 0);
            if (this.handle == IntPtr.Zero)
            {
                // Small trick to get exact error message...
                try {
                    using (FileStream writer = File.Create(path)) {
                        writer.WriteByte(0);
                    }
                    File.Delete(path);
                    throw new ZipCreationException();
                } catch (Exception ex) {
                    throw new ZipCreationException(ex.Message);
                }
            }
            this.entryOpened = false;
        }
Ejemplo n.º 4
0
        public override Stream GetEntry(string relativePath)
        {
            string resolvedPath = ZipLib.ResolvePath(relativePath);

            if (ZipLib.unzLocateFile(this.handle, resolvedPath, 0) != 0)
            {
                throw new ZipEntryNotFoundException("Entry not found:" + relativePath);
            }

            ZipEntryInfo entryInfo = new ZipEntryInfo();
            int          result    = ZipLib.unzGetCurrentFileInfo(this.handle, out entryInfo, null, UIntPtr.Zero, null, UIntPtr.Zero, null, UIntPtr.Zero);

            if (result != 0)
            {
                throw new ZipException("Error while reading entry info: " + relativePath + " - Errorcode: " + result);
            }

            result = ZipLib.unzOpenCurrentFile(this.handle);
            if (result != 0)
            {
                throw new ZipException("Error while opening entry: " + relativePath + " - Errorcode: " + result);
            }

            byte[] buffer    = new byte[entryInfo.UncompressedSize.ToUInt64()];
            int    bytesRead = 0;

            if ((bytesRead = ZipLib.unzReadCurrentFile(this.handle, buffer, (uint)entryInfo.UncompressedSize)) < 0)
            {
                throw new ZipException("Error while reading entry: " + relativePath + " - Errorcode: " + result);
            }

            result = ZipLib.unzCloseCurrentFile(handle);
            if (result != 0)
            {
                throw new ZipException("Error while closing entry: " + relativePath + " - Errorcode: " + result);
            }

            return(new MemoryStream(buffer, 0, bytesRead));
        }