/// <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);
        }
        /// <summary>
        /// 压缩文件和文件夹;
        /// </summary>
        /// <param name="fileOrDirectoryArray">文件夹路径和文件名</param>
        /// <param name="outputPathName">压缩后的输出路径文件名</param>
        /// <param name="password">压缩密码</param>
        /// <param name="zipCallback">ZipCallback对象,负责回调</param>
        /// <returns></returns>
        public static bool Zip(string[] fileOrDirectoryArray, string outputPathName,
                               string password = null, ZipCallback zipCallback = null)
        {
            if ((null == fileOrDirectoryArray) || string.IsNullOrEmpty(outputPathName))
            {
                if (null != zipCallback)
                {
                    zipCallback.OnFinished(false);
                }

                return(false);
            }

            var zipOutputStream = new ZipOutputStream(File.Create(outputPathName));

            zipOutputStream.SetLevel(6);    // 压缩质量和压缩速度的平衡点;
            if (!string.IsNullOrEmpty(password))
            {
                zipOutputStream.Password = password;
            }

            for (var index = 0; index < fileOrDirectoryArray.Length; ++index)
            {
                var result          = false;
                var fileOrDirectory = fileOrDirectoryArray[index];
                if (Directory.Exists(fileOrDirectory))
                {
                    result = ZipDirectory(fileOrDirectory, string.Empty, zipOutputStream, zipCallback);
                }
                else if (File.Exists(fileOrDirectory))
                {
                    result = ZipFile(fileOrDirectory, string.Empty, zipOutputStream, zipCallback);
                }
                if (!result)
                {
                    if (null != zipCallback)
                    {
                        zipCallback.OnFinished(false);
                    }

                    return(false);
                }
            }

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

            if (null != zipCallback)
            {
                zipCallback.OnFinished(true);
            }

            return(true);
        }
        /// <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);
        }
Exemple #4
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);
    }
Exemple #5
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);
    }
Exemple #6
0
    //压缩文件和文件夹
    public static bool Zip(string[] pFileOrDirArray,    //需要压缩的文件和文件夹
                           string pZipFilePath,         //输出的zip文件完整路径
                           string pPassword = null,     //密码
                           ZipCallback pCB  = null,     //回调
                           int pZipLevel    = 6)        //压缩等级
    {
        if (null == pFileOrDirArray || string.IsNullOrEmpty(pZipFilePath))
        {
            pCB?.OnFinished(false);
            return(false);
        }

        var zipOutputStream = new ZipOutputStream(File.Create(pZipFilePath));

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

        foreach (string fileOrDirectory in pFileOrDirArray)
        {
            var result = false;

            if (Directory.Exists(fileOrDirectory))
            {
                result = ZipDirectory(fileOrDirectory, string.Empty, zipOutputStream, pCB);
            }
            else if (File.Exists(fileOrDirectory))
            {
                result = ZipFile(fileOrDirectory, string.Empty, zipOutputStream, pCB);
            }

            if (!result)
            {
                GC.Collect();
                pCB?.OnFinished(false);
                return(false);
            }
        }

        zipOutputStream.Finish();
        zipOutputStream.Close();
        zipOutputStream = null;

        GC.Collect();
        pCB?.OnFinished(true);
        return(true);
    }
        /// <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);    // 过滤
                }
                _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);
        }
        /// <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);
                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);
        }
        /// <summary>
        /// 压缩文件和文件夹
        /// </summary>
        /// <param name="_fileOrDirectoryArray">文件夹路径和文件名</param>
        /// <param name="_outputPathName">压缩后的输出路径文件名</param>
        /// <param name="_password">压缩密码</param>
        /// <param name="_zipCallback">ZipCallback对象,负责回调</param>
        /// <returns></returns>
        public static bool Zip(string[] _fileOrDirectoryArray, string _outputPathName, string _password = null, ZipCallback _zipCallback = null)
        {
            if ((null == _fileOrDirectoryArray) || string.IsNullOrEmpty(_outputPathName))
            {
                if (null != _zipCallback)
                {
                    _zipCallback.OnFinished(false);
                }

                return(false);
            }

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

            zipOutputStream.SetLevel(6);    // 压缩质量和压缩速度的平衡点
            if (!string.IsNullOrEmpty(_password))
            {
                zipOutputStream.Password = _password;
            }

            for (int index = 0; index < _fileOrDirectoryArray.Length; ++index)
            {
                bool   result          = false;
                string fileOrDirectory = _fileOrDirectoryArray[index];
                if (Directory.Exists(fileOrDirectory))
                {
                    result = ZipDirectory(fileOrDirectory, string.Empty, zipOutputStream, _zipCallback);
                }
                else if (File.Exists(fileOrDirectory))
                {
                    result = ZipFile(fileOrDirectory, string.Empty, zipOutputStream, _zipCallback);
                }

                if (!result)
                {
                    if (null != _zipCallback)
                    {
                        _zipCallback.OnFinished(false);
                    }

                    return(false);
                }
            }

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

            if (null != _zipCallback)
            {
                _zipCallback.OnFinished(true);
            }

            return(true);
        }
Exemple #10
0
    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);
    }
Exemple #11
0
        /// <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);
        }