Exemplo n.º 1
0
        private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName, bool separate, string[] files, string[] dirs)
        {
            bool res = true;

            string[]   folders, filenames;
            ZipEntry   entry = null;
            FileStream fs    = null;
            Crc32      crc   = new Crc32();

            try
            {
                //创建当前文件夹
                entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才会当成是文件夹创建
                s.PutNextEntry(entry);
                s.Flush();

                //先压缩文件,再递归压缩文件夹
                if (separate)
                {
                    filenames = files;
                }
                else
                {
                    filenames = Directory.GetFiles(FolderToZip);
                }

                foreach (string file in filenames)
                {
                    //打开压缩文件
                    fs = File.OpenRead(file);

                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string p = Path.GetFileName(FolderToZip);

                    if (p.Length < 1)
                    {
                        p = Path.GetFileName(file);
                    }
                    else
                    {
                        p += "/" + Path.GetFileName(file);
                    }

                    entry = new ZipEntry(Path.Combine(ParentFolderName, p));

                    entry.DateTime = DateTime.Now;
                    entry.Size     = fs.Length;
                    fs.Close();

                    crc.Reset();
                    crc.Update(buffer);

                    entry.Crc = crc.Value;

                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }

                if (entry != null)
                {
                    entry = null;
                }

                GC.Collect();
                GC.Collect(1);
            }

            if (separate)
            {
                folders = dirs;
            }
            else
            {
                folders = Directory.GetDirectories(FolderToZip);
            }

            foreach (string folder in folders)
            {
                if (folder.Equals(BackUpPath, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }
                else if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip)), false, null, null))
                {
                    return(false);
                }
            }

            return(res);
        }
Exemplo n.º 2
0
        void ReadHeader()
        {
            /* 1. Check the two magic bytes */
            Crc32 headCRC = new Crc32();
            int   magic   = baseInputStream.ReadByte();

            if (magic < 0)
            {
                eos = true;
                return;
            }
            headCRC.Update(magic);
            if (magic != (GZipConstants.GZIP_MAGIC >> 8))
            {
                throw new IOException("Error baseInputStream GZIP header, first byte doesn't match");
            }

            magic = baseInputStream.ReadByte();
            if (magic != (GZipConstants.GZIP_MAGIC & 0xFF))
            {
                throw new IOException("Error baseInputStream GZIP header,  second byte doesn't match");
            }
            headCRC.Update(magic);

            /* 2. Check the compression type (must be 8) */
            int CM = baseInputStream.ReadByte();

            if (CM != 8)
            {
                throw new IOException("Error baseInputStream GZIP header, data not baseInputStream deflate format");
            }
            headCRC.Update(CM);

            /* 3. Check the flags */
            int flags = baseInputStream.ReadByte();

            if (flags < 0)
            {
                throw new Exception("Early EOF baseInputStream GZIP header");
            }
            headCRC.Update(flags);

            /*    This flag byte is divided into individual bits as follows:
             *
             *  bit 0   FTEXT
             *  bit 1   FHCRC
             *  bit 2   FEXTRA
             *  bit 3   FNAME
             *  bit 4   FCOMMENT
             *  bit 5   reserved
             *  bit 6   reserved
             *  bit 7   reserved
             */

            /* 3.1 Check the reserved bits are zero */

            if ((flags & 0xd0) != 0)
            {
                throw new IOException("Reserved flag bits baseInputStream GZIP header != 0");
            }

            /* 4.-6. Skip the modification time, extra flags, and OS type */
            for (int i = 0; i < 6; i++)
            {
                int readByte = baseInputStream.ReadByte();
                if (readByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }
                headCRC.Update(readByte);
            }

            /* 7. Read extra field */
            if ((flags & GZipConstants.FEXTRA) != 0)
            {
                /* Skip subfield id */
                for (int i = 0; i < 2; i++)
                {
                    int readByte = baseInputStream.ReadByte();
                    if (readByte < 0)
                    {
                        throw new Exception("Early EOF baseInputStream GZIP header");
                    }
                    headCRC.Update(readByte);
                }
                if (baseInputStream.ReadByte() < 0 || baseInputStream.ReadByte() < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }

                int len1, len2, extraLen;
                len1 = baseInputStream.ReadByte();
                len2 = baseInputStream.ReadByte();
                if ((len1 < 0) || (len2 < 0))
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }
                headCRC.Update(len1);
                headCRC.Update(len2);

                extraLen = (len1 << 8) | len2;
                for (int i = 0; i < extraLen; i++)
                {
                    int readByte = baseInputStream.ReadByte();
                    if (readByte < 0)
                    {
                        throw new Exception("Early EOF baseInputStream GZIP header");
                    }
                    headCRC.Update(readByte);
                }
            }

            /* 8. Read file name */
            if ((flags & GZipConstants.FNAME) != 0)
            {
                int readByte;
                while ((readByte = baseInputStream.ReadByte()) > 0)
                {
                    headCRC.Update(readByte);
                }
                if (readByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP file name");
                }
                headCRC.Update(readByte);
            }

            /* 9. Read comment */
            if ((flags & GZipConstants.FCOMMENT) != 0)
            {
                int readByte;
                while ((readByte = baseInputStream.ReadByte()) > 0)
                {
                    headCRC.Update(readByte);
                }

                if (readByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP comment");
                }
                headCRC.Update(readByte);
            }

            /* 10. Read header CRC */
            if ((flags & GZipConstants.FHCRC) != 0)
            {
                int tempByte;
                int crcval = baseInputStream.ReadByte();
                if (crcval < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }

                tempByte = baseInputStream.ReadByte();
                if (tempByte < 0)
                {
                    throw new Exception("Early EOF baseInputStream GZIP header");
                }

                crcval = (crcval << 8) | tempByte;
                if (crcval != ((int)headCRC.Value & 0xffff))
                {
                    throw new IOException("Header CRC value mismatch");
                }
            }

            readGZIPHeader = true;
            //System.err.println("Read GZIP header");
        }
Exemplo n.º 3
0
        private void WriteChunk(Stream stream, string type, byte[] data, int offset, int length)
        {
            WriteInteger(stream, length);

            byte[] typeArray = new byte[4];
            typeArray[0] = (byte)type[0];
            typeArray[1] = (byte)type[1];
            typeArray[2] = (byte)type[2];
            typeArray[3] = (byte)type[3];

            stream.Write(typeArray, 0, 4);

            if (data != null)
            {
                stream.Write(data, offset, length);
            }

            Crc32 crc32 = new Crc32();
            crc32.Update(typeArray);

            if (data != null)
            {
                crc32.Update(data, offset, length);
            }

            WriteInteger(stream, (uint)crc32.Value);
        }
Exemplo n.º 4
0
            private void ReadChunkCrc(PngChunk chunk, byte[] typeBuffer)
            {
                byte[] crcBuffer = new byte[4];

                int numBytes = _stream.Read(crcBuffer, 0, 4);
                if (numBytes >= 1 && numBytes <= 3)
                {
                    throw new ImageFormatException("Image stream is not valid!");
                }

                Array.Reverse(crcBuffer);

                chunk.Crc = BitConverter.ToUInt32(crcBuffer, 0);

                Crc32 crc = new Crc32();
                crc.Update(typeBuffer);
                crc.Update(chunk.Data);

                if (crc.Value != chunk.Crc)
                {
                    throw new ImageFormatException("CRC Error. PNG Image chunk is corrupt!");
                }
            }
Exemplo n.º 5
0
        /// <summary>
        /// Reads a block of bytes from the current zip entry.
        /// </summary>
        /// <returns>
        /// The number of bytes read (this may be less than the length requested, even before the end of stream), or 0 on end of stream.
        /// </returns>
        /// <exception name="IOException">
        /// An i/o error occured.
        /// </exception>
        /// <exception cref="ZipException">
        /// The deflated stream is corrupted.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// The stream is not open.
        /// </exception>
        int BodyRead(byte[] buffer, int offset, int count)
        {
            if (crc == null)
            {
                throw new InvalidOperationException("Closed");
            }

            if ((entry == null) || (count <= 0))
            {
                return(0);
            }

            if (offset + count > buffer.Length)
            {
                throw new ArgumentException("Offset + count exceeds buffer size");
            }

            bool finished = false;

            switch (method)
            {
            case (int)CompressionMethod.Deflated:
                count = base.Read(buffer, offset, count);
                if (count <= 0)
                {
                    if (!inf.IsFinished)
                    {
                        throw new ZipException("Inflater not finished!");
                    }
                    inputBuffer.Available = inf.RemainingInput;

                    // A csize of -1 is from an unpatched local header
                    if ((flags & 8) == 0 &&
                        (inf.TotalIn != csize && csize != 0xFFFFFFFF && csize != -1 || inf.TotalOut != size))
                    {
                        throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
                    }
                    inf.Reset();
                    finished = true;
                }
                break;

            case (int)CompressionMethod.Stored:
                if ((count > csize) && (csize >= 0))
                {
                    count = (int)csize;
                }

                if (count > 0)
                {
                    count = inputBuffer.ReadClearTextBuffer(buffer, offset, count);
                    if (count > 0)
                    {
                        csize -= count;
                        size  -= count;
                    }
                }

                if (csize == 0)
                {
                    finished = true;
                }
                else
                {
                    if (count < 0)
                    {
                        throw new ZipException("EOF in stored block");
                    }
                }
                break;
            }

            if (count > 0)
            {
                crc.Update(buffer, offset, count);
            }

            if (finished)
            {
                CompleteCloseEntry(true);
            }

            return(count);
        }