コード例 #1
0
    /// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="_filePathName">文件路径名</param>
    /// <param name="_parentRelPath">要压缩的文件的父相对文件夹</param>
    /// <param name="_zipOutputStream">压缩输出流</param>
    /// <param name="_zipCallback">ZipCallback对象,负责回调</param>
    /// <returns></returns>
    private static bool ZipFile(string _filePathName, string _parentRelPath, ZipOutputStream _zipOutputStream, ZipCallback _zipCallback = null)
    {
        //Crc32 crc32 = new Crc32();
        ZipEntry   entry      = null;
        FileStream fileStream = null;

        try
        {
            string entryName = _parentRelPath + '/' + Path.GetFileName(_filePathName);
            if (_parentRelPath == null || _parentRelPath.Equals(""))
            {
                entryName = Path.GetFileName(_filePathName);
            }

            entry          = new ZipEntry(entryName);
            entry.DateTime = System.DateTime.Now;

            if ((null != _zipCallback) && !_zipCallback.OnPreZip(entry))
            {
                return(true);    // 过滤
            }
            fileStream = File.OpenRead(_filePathName);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
            fileStream.Close();

            entry.Size = buffer.Length;

            //crc32.Reset();
            //crc32.Update(buffer);
            //entry.Crc = crc32.Value;

            _zipOutputStream.PutNextEntry(entry);
            _zipOutputStream.Write(buffer, 0, buffer.Length);
        }
        catch (System.Exception _e)
        {
            Debug.LogError("[ZipUtility.ZipFile]: " + _e.ToString());
            return(false);
        }
        finally
        {
            if (null != fileStream)
            {
                fileStream.Close();
                fileStream.Dispose();
            }
        }

        if (null != _zipCallback)
        {
            _zipCallback.OnPostZip(entry);
        }

        return(true);
    }
コード例 #2
0
        /// <summary>
        /// 压缩文件;
        /// </summary>
        /// <param name="filePathName">文件路径名</param>
        /// <param name="parentRelPath">要压缩的文件的父相对文件夹</param>
        /// <param name="zipOutputStream">压缩输出流</param>
        /// <param name="zipCallback">ZipCallback对象,负责回调</param>
        /// <returns></returns>
        private static bool ZipFile(string filePathName, string parentRelPath,
                                    ZipOutputStream zipOutputStream, ZipCallback zipCallback = null)
        {
            //Crc32 crc32 = new Crc32();
            ZipEntry   entry      = null;
            FileStream fileStream = null;

            try
            {
                var entryName = parentRelPath + '/' + Path.GetFileName(filePathName);
                entry = new ZipEntry(entryName)
                {
                    DateTime = System.DateTime.Now
                };

                if ((null != zipCallback) && !zipCallback.OnPreZip(entry))
                {
                    return(true);    // 过滤;
                }

                fileStream = File.OpenRead(filePathName);
                var buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, buffer.Length);
                fileStream.Close();

                entry.Size = buffer.Length;

                //crc32.Reset();
                //crc32.Update(buffer);
                //entry.Crc = crc32.Value;

                zipOutputStream.PutNextEntry(entry);
                zipOutputStream.Write(buffer, 0, buffer.Length);
            }
            catch (System.Exception e)
            {
                LogHelper.PrintError($"[SharpZipLibHelper.ZipFile]: {e.ToString()}");
                return(false);
            }
            finally
            {
                if (null != fileStream)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                }
            }

            if (null != zipCallback)
            {
                zipCallback.OnPostZip(entry);
            }

            return(true);
        }
コード例 #3
0
        /// <summary>
        /// 压缩文件夹;
        /// </summary>
        /// <param name="path">要压缩的文件夹</param>
        /// <param name="parentRelPath">要压缩的文件夹的父相对文件夹</param>
        /// <param name="zipOutputStream">压缩输出流</param>
        /// <param name="zipCallback">ZipCallback对象,负责回调</param>
        /// <returns></returns>
        private static bool ZipDirectory(string path, string parentRelPath,
                                         ZipOutputStream zipOutputStream, ZipCallback zipCallback = null)
        {
            ZipEntry entry = null;

            try
            {
                var entryName = Path.Combine(parentRelPath, Path.GetFileName(path) + '/');
                entry = new ZipEntry(entryName)
                {
                    DateTime = System.DateTime.Now,
                    Size     = 0
                };

                if ((null != zipCallback) && !zipCallback.OnPreZip(entry))
                {
                    return(true);    // 过滤;
                }

                zipOutputStream.PutNextEntry(entry);
                zipOutputStream.Flush();

                var files = Directory.GetFiles(path);
                for (var index = 0; index < files.Length; ++index)
                {
                    ZipFile(files[index], Path.Combine(parentRelPath, Path.GetFileName(path)), zipOutputStream, zipCallback);
                }
            }
            catch (System.Exception e)
            {
                LogHelper.PrintError($"[SharpZipLibHelper.ZipDirectory]: {e.ToString()}");
                return(false);
            }

            var directories = Directory.GetDirectories(path);

            for (var index = 0; index < directories.Length; ++index)
            {
                if (!ZipDirectory(directories[index], Path.Combine(parentRelPath, Path.GetFileName(path)), zipOutputStream, zipCallback))
                {
                    return(false);
                }
            }

            if (null != zipCallback)
            {
                zipCallback.OnPostZip(entry);
            }
            return(true);
        }
コード例 #4
0
    /// <summary>
    /// 压缩文件夹
    /// </summary>
    /// <param name="_path">要压缩的文件夹</param>
    /// <param name="_parentRelPath">要压缩的文件夹的父相对文件夹</param>
    /// <param name="_zipOutputStream">压缩输出流</param>
    /// <param name="_zipCallback">ZipCallback对象,负责回调</param>
    /// <returns></returns>
    private static bool ZipDirectory(string _path, string _parentRelPath, ZipOutputStream _zipOutputStream, ZipCallback _zipCallback = null)
    {
        ZipEntry entry = null;

        try
        {
            string entryName = Path.Combine(_parentRelPath, Path.GetFileName(_path) + '/');
            entry          = new ZipEntry(entryName);
            entry.DateTime = System.DateTime.Now;
            entry.Size     = 0;

            if ((null != _zipCallback) && !_zipCallback.OnPreZip(entry))
            {
                return(true);    // 过滤
            }
            if (!entryName.Equals("/"))
            {
                _zipOutputStream.PutNextEntry(entry);
                _zipOutputStream.Flush();
            }


            string[] files = Directory.GetFiles(_path);
            for (int index = 0; index < files.Length; ++index)
            {
                ZipFile(files[index], Path.Combine(_parentRelPath, Path.GetFileName(_path)), _zipOutputStream, _zipCallback);
            }
        }
        catch (System.Exception _e)
        {
            Debug.LogError("[ZipUtility.ZipDirectory]: " + _e.ToString());
            return(false);
        }

        string[] directories = Directory.GetDirectories(_path);
        for (int index = 0; index < directories.Length; ++index)
        {
            if (!ZipDirectory(directories[index], Path.Combine(_parentRelPath, Path.GetFileName(_path)), _zipOutputStream, _zipCallback))
            {
                return(false);
            }
        }

        if (null != _zipCallback)
        {
            _zipCallback.OnPostZip(entry);
        }

        return(true);
    }
コード例 #5
0
    //压缩文件夹
    private static bool ZipDirectory(string pDirPath,                   //文件夹路径
                                     string pParentPath,                //相对路径
                                     ZipOutputStream pZipOutputStream,  //压缩输出流
                                     ZipCallback pCB = null)            //回调
    {
        ZipEntry entry = null;
        string   path  = Path.Combine(pParentPath, GetDirName(pDirPath));

        try
        {
            entry = new ZipEntry(path)
            {
                DateTime = DateTime.Now,
                Size     = 0
            };

            if (null != pCB && !pCB.OnPreZip(entry))
            {
                return(true); // 过滤
            }
            pZipOutputStream.PutNextEntry(entry);
            pZipOutputStream.Flush();

            var files = Directory.GetFiles(pDirPath);
            foreach (string file in files)
            {
                ZipFile(file, Path.Combine(pParentPath, GetDirName(pDirPath)), pZipOutputStream, pCB);
            }
        }
        catch (Exception _e)
        {
            Debug.LogError("[ZipDirectory]: " + _e);
            return(false);
        }

        var directories = Directory.GetDirectories(pDirPath);

        foreach (string dir in directories)
        {
            if (!ZipDirectory(dir, Path.Combine(pParentPath, GetDirName(pDirPath)), pZipOutputStream, pCB))
            {
                return(false);
            }
        }

        pCB?.OnPostZip(entry);

        return(true);
    }
コード例 #6
0
    //压缩文件
    private static bool ZipFile(string pFileName,                   //需要压缩的文件名
                                string pParentPath,                 //相对路径
                                ZipOutputStream pZipOutputStream,   //压缩输出流
                                ZipCallback pCB = null)             //回调
    {
        ZipEntry   entry      = null;
        FileStream fileStream = null;

        try
        {
            string path = pParentPath + Path.GetFileName(pFileName);
            entry = new ZipEntry(path)
            {
                DateTime = DateTime.Now
            };

            if (null != pCB && !pCB.OnPreZip(entry))
            {
                return(true); // 过滤
            }
            fileStream = File.OpenRead(pFileName);
            var buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
            fileStream.Close();

            entry.Size = buffer.Length;

            pZipOutputStream.PutNextEntry(entry);
            pZipOutputStream.Write(buffer, 0, buffer.Length);
        }
        catch (Exception _e)
        {
            Debug.LogError("[ZipUtility.ZipFile]: " + _e);
            return(false);
        }
        finally
        {
            if (null != fileStream)
            {
                fileStream.Close();
                fileStream.Dispose();
            }
        }

        pCB?.OnPostZip(entry);

        return(true);
    }
コード例 #7
0
ファイル: ModCompress.cs プロジェクト: SES-xuelan/XLAF
        /// <summary>
        /// compress folder
        /// </summary>
        /// <param name="path">folder path</param>
        /// <param name="parentRelPath">parent path</param>
        /// <param name="zipOutputStream">zipOutputStream</param>
        /// <param name="zipCallback">ZipCallback</param>
        /// <returns></returns>
        private static bool ZipDirectory(string path, string parentRelPath, ZipOutputStream zipOutputStream, ZipCallback zipCallback = null)
        {
            ZipEntry entry = null;

            try {
                string entryName = Path.Combine(parentRelPath, Path.GetFileName(path) + '/');
                entry          = new ZipEntry(entryName);
                entry.DateTime = System.DateTime.Now;
                entry.Size     = 0;

                if ((null != zipCallback) && !zipCallback.OnPreZip(entry))
                {
                    return(true);                       // filter
                }
                zipOutputStream.PutNextEntry(entry);
                zipOutputStream.Flush();

                string[] files = Directory.GetFiles(path);
                for (int index = 0; index < files.Length; ++index)
                {
                    ZipFile(files [index], Path.Combine(parentRelPath, Path.GetFileName(path)), zipOutputStream, zipCallback);
                }
            } catch (System.Exception e) {
                Debug.LogError(e.ToString());
                return(false);
            }

            string[] directories = Directory.GetDirectories(path);
            for (int index = 0; index < directories.Length; ++index)
            {
                if (!ZipDirectory(directories [index], Path.Combine(parentRelPath, Path.GetFileName(path)), zipOutputStream, zipCallback))
                {
                    return(false);
                }
            }

            if (null != zipCallback)
            {
                zipCallback.OnPostZip(entry);
            }

            return(true);
        }
コード例 #8
0
ファイル: ModCompress.cs プロジェクト: SES-xuelan/XLAF
        /// <summary>
        /// compress file
        /// </summary>
        /// <param name="filePathName">file path name</param>
        /// <param name="parentRelPath">parent path</param>
        /// <param name="zipOutputStream">zipOutputStream</param>
        /// <param name="zipCallback">ZipCallback</param>
        /// <returns></returns>
        private static bool ZipFile(string filePathName, string parentRelPath, ZipOutputStream zipOutputStream, ZipCallback zipCallback = null)
        {
            ZipEntry   entry      = null;
            FileStream fileStream = null;

            try {
                string entryName = parentRelPath + '/' + Path.GetFileName(filePathName);
                entry          = new ZipEntry(entryName);
                entry.DateTime = System.DateTime.Now;

                if ((null != zipCallback) && !zipCallback.OnPreZip(entry))
                {
                    return(true);                       // filter
                }
                fileStream = File.OpenRead(filePathName);
                byte[] buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, buffer.Length);
                fileStream.Close();

                entry.Size = buffer.Length;

                zipOutputStream.PutNextEntry(entry);
                zipOutputStream.Write(buffer, 0, buffer.Length);
            } catch (System.Exception e) {
                Debug.LogError(e.ToString());
                return(false);
            } finally {
                if (null != fileStream)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                }
            }

            if (null != zipCallback)
            {
                zipCallback.OnPostZip(entry);
            }

            return(true);
        }
コード例 #9
0
ファイル: ZipUtility.cs プロジェクト: xzp3094997376/FBXLoad
    public static bool ZipBytes(byte[] _bytes, string _outputPathName, string _password = null, ZipCallback _zipCallback = null)
    {
        if ((null == _bytes) || string.IsNullOrEmpty(_outputPathName))
        {
            Debug.Log(_bytes + "  文件流为空");
            return(false);
        }
        if (!Directory.Exists(_outputPathName))
        {
            Directory.CreateDirectory(_outputPathName);
        }

        Debug.Log(_outputPathName);
        _outputPathName = PathRespace(_outputPathName);
        string _fileName = Path.Combine(_outputPathName, "ttjkz.zip");

        _fileName = PathRespace(_fileName);

        Debug.LogError(_fileName);

        ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(_fileName));

        zipOutputStream.SetLevel(6);    // 压缩质量和压缩速度的平衡点


        ZipEntry   entry      = null;
        FileStream fileStream = null;

        try
        {
            string entryName = Path.Combine("/", Path.GetFileName(_fileName));

            entryName = PathRespace(entryName);
            Debug.Log("entryName:" + entryName);
            entry          = new ZipEntry(entryName);
            entry.DateTime = System.DateTime.Now;

            if ((null != _zipCallback) && !_zipCallback.OnPreZip(entry))
            {
                return(true);    // 过滤
            }
            entry.Size = _bytes.Length;

            //crc32.Reset();
            //crc32.Update(buffer);
            //entry.Crc = crc32.Value;

            zipOutputStream.PutNextEntry(entry);
            zipOutputStream.Write(_bytes, 0, _bytes.Length);
        }
        catch (System.Exception _e)
        {
            Debug.LogError("[ZipUtility.ZipFile]: " + _e.ToString());
            return(false);
        }
        finally
        {
            if (null != fileStream)
            {
                fileStream.Flush();
                fileStream.Close();
                fileStream.Dispose();
            }
        }

        if (null != _zipCallback)
        {
            _zipCallback.OnPostZip(entry);
        }

        //return true;
        zipOutputStream.Flush();

        zipOutputStream.Finish();
        zipOutputStream.Close();

        zipOutputStream.Dispose();

        return(true);
    }