Exemple #1
0
        public long AddFileData(byte[] data, int pos, int len)
        {
            long totalSize = currentFile.CurrentSize + len;

            if (totalSize > currentFile.Size)
            {
                throw new ApplicationException("totalSize > currentFile.Size");
            }

            if (currentFile.Compress == false)
            {
                fifo.Write(data, pos, len);
            }
            else
            {
                CoreUtil.Internal.ZStream zs = currentFile.ZStream;

                byte[] srcData = Util.ExtractByteArray(data, pos, len);
                byte[] dstData = new byte[srcData.Length * 2 + 100];

                zs.next_in       = srcData;
                zs.avail_in      = srcData.Length;
                zs.next_in_index = 0;

                zs.next_out       = dstData;
                zs.avail_out      = dstData.Length;
                zs.next_out_index = 0;

                if (currentFile.Size == (currentFile.CurrentSize + len))
                {
                    zs.deflate(CoreUtil.Internal.zlibConst.Z_FINISH);
                }
                else
                {
                    zs.deflate(CoreUtil.Internal.zlibConst.Z_SYNC_FLUSH);
                }

                fifo.Write(dstData, 0, dstData.Length - zs.avail_out);

                currentFile.CompressSize += dstData.Length - zs.avail_out;

                Util.NoOP();
            }

            currentFile.CurrentSize += len;

            currentFile.Crc32 = ZipUtil.Crc32Next(data, pos, len, currentFile.Crc32);

            long ret = currentFile.Size - currentFile.CurrentSize;

            if (ret == 0)
            {
                currentFile.Crc32 = ~currentFile.Crc32;
                addFileFooter();

                currentFile = null;
            }

            return(ret);
        }
Exemple #2
0
        public void Write(byte[] data, int pos, int len, bool finish)
        {
            byte[] srcData = Util.ExtractByteArray(data, pos, len);
            byte[] dstData = new byte[srcData.Length * 2 + 100];

            if (this.finished)
            {
                throw new ApplicationException("already finished");
            }

            zs.next_in       = srcData;
            zs.avail_in      = srcData.Length;
            zs.next_in_index = 0;

            zs.next_out       = dstData;
            zs.avail_out      = dstData.Length;
            zs.next_out_index = 0;

            if (finish)
            {
                zs.deflate(zlibConst.Z_FINISH);
            }
            else
            {
                zs.deflate(zlibConst.Z_SYNC_FLUSH);
            }

            fifo.Write(dstData, 0, dstData.Length - zs.avail_out);

            currentSize += len;

            this.crc32 = ZipUtil.Crc32Next(data, pos, len, this.crc32);

            if (finish)
            {
                this.finished = true;
                this.crc32    = ~this.crc32;

                GZipFooter f = new GZipFooter();
                f.CRC32 = this.crc32;
                f.ISIZE = (uint)(this.currentSize % 0x100000000);

                fifo.Write(Util.StructToByte(f));
            }
        }
Exemple #3
0
        public static void FileCopy(string oldName, string newName, bool skipIfNoChange, bool deleteBom, bool useTimeStampToCheckNoChange)
        {
            string tmp1 = InnerFilePath(oldName);
            string tmp2 = InnerFilePath(newName);

            if (useTimeStampToCheckNoChange && skipIfNoChange)
            {
                DateTime dt1, dt2;

                try
                {
                    dt1 = Directory.GetLastWriteTimeUtc(tmp1);
                    dt2 = Directory.GetLastWriteTimeUtc(tmp2);

                    TimeSpan ts = dt2 - dt1;
                    if (ts.TotalSeconds >= -5.0)
                    {
                        return;
                    }
                }
                catch
                {
                }
            }

            if (skipIfNoChange || deleteBom)
            {
                byte[] srcData  = File.ReadAllBytes(tmp1);
                byte[] destData = new byte[0];
                bool   changed  = true;
                int    bomSize;

                Str.GetEncoding(srcData, out bomSize);
                if (bomSize >= 1)
                {
                    srcData = Util.ExtractByteArray(srcData, bomSize, srcData.Length - bomSize);
                }

                if (skipIfNoChange)
                {
                    try
                    {
                        FileStream fs   = File.OpenRead(tmp2);
                        long       size = 0xffffffff;
                        try
                        {
                            size = fs.Length;
                        }
                        finally
                        {
                            fs.Close();
                        }

                        if (size == srcData.Length || srcData.Length == 0)
                        {
                            destData = File.ReadAllBytes(tmp2);
                        }
                    }
                    catch
                    {
                    }

                    if (Util.CompareByte(srcData, destData))
                    {
                        changed = false;
                    }
                }

                if (changed)
                {
                    File.WriteAllBytes(tmp2, srcData);
                    CopyFileTimestamp(tmp2, tmp1);
                }
            }
            else
            {
                File.Copy(tmp1, tmp2, true);
            }
        }