Ejemplo n.º 1
0
 public override void Close()
 {
     if (handle != IntPtr.Zero)
     {
         int result = ZipLib.unzClose(this.handle);
         handle = IntPtr.Zero;
         // Question: should we raise this exception ?
         if (result != 0)
         {
             throw new ZipException("Error closing file - Errorcode: " + result);
         }
     }
 }
Ejemplo n.º 2
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.º 3
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            int result;

            if (offset != 0)
            {
                byte[] newBuffer = new byte[count];
                Array.Copy(buffer, offset, newBuffer, 0, count);
                result = ZipLib.zipWriteInFileInZip(handle, newBuffer, (uint)count);
            }
            else
            {
                result = ZipLib.zipWriteInFileInZip(handle, buffer, (uint)count);
            }

            if (result < 0)
            {
                throw new ZipException("Error while writing entry - Errorcode: " + result);
            }
        }
Ejemplo n.º 4
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.º 5
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.º 6
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));
        }
Ejemplo n.º 7
0
 public override void Close()
 {
     if (handle != IntPtr.Zero)
     {
         int result;
         if (this.entryOpened)
         {
             result = ZipLib.zipCloseFileInZip(this.handle);
             if (result != 0)
             {
                 throw new ZipException("Error while closing entry - Errorcode: " + result);
             }
             this.entryOpened = false;
         }
         result = ZipLib.zipClose(this.handle, "");
         handle = IntPtr.Zero;
         // Should we raise this exception ?
         if (result != 0)
         {
             throw new ZipException("Error while closing ZIP file - Errorcode: " + result);
         }
     }
 }