コード例 #1
0
        public static List <TreeNode> ZipList(byte[] bytes)
        {
            List <ZipItem> result          = new List <ZipItem>();
            Stream         ExistingZipFile = new MemoryStream(bytes);// 把 byte[] 转换成 Stream
            ReadOptions    readoption      = new ReadOptions();

            readoption.Encoding = Encoding.GetEncoding("GB2312");
            using (ZipFile zip = ZipFile.Read(ExistingZipFile, readoption))
            {
                foreach (ZipEntry e in zip)
                {
                    ZipItem  zipitem  = new ZipItem();
                    string   filename = e.FileName;
                    string[] ary      = filename.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    int      length   = ary.Length;
                    zipitem.path        = filename;
                    zipitem.rank        = length;
                    zipitem.name        = ary[length - 1];
                    zipitem.isdirectory = e.IsDirectory;
                    if (zipitem != null && !result.Contains(zipitem))
                    {
                        result.Add(zipitem);
                    }
                }
            }
            List <TreeNode> nodeList = TreeDiTui(result, GetfirstDirectory(result, 1));

            return(nodeList);
        }
コード例 #2
0
        /// <summary>
        /// 获取zip文件的目录树
        /// </summary>
        /// <param name="path">文件绝对路径</param>
        /// <param name="isExtract">是否解压zip包,默认不解压</param>
        /// <returns>目录树json</returns>
        public static List <TreeNode> ZipList(string path, string destPath, bool isExtract = false)
        {
            List <ZipItem> result     = new List <ZipItem>();
            FileStream     fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); // 打开文件

            byte[] bytes = new byte[fileStream.Length];                                                       // 读取文件的 byte[]
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            Stream      ExistingZipFile = new MemoryStream(bytes);// 把 byte[] 转换成 Stream
            ReadOptions readoption      = new ReadOptions();

            readoption.Encoding = Encoding.GetEncoding("GB2312");
            using (ZipFile zip = ZipFile.Read(ExistingZipFile, readoption))
            {
                #region 判断是否解压文件夹
                if (isExtract)
                {
                    zip.ExtractAll(destPath, ExtractExistingFileAction.OverwriteSilently);
                }
                #endregion

                foreach (ZipEntry e in zip)
                {
                    ZipItem  zipitem  = new ZipItem();
                    string   filename = e.FileName;
                    string[] ary      = filename.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    int      length   = ary.Length;
                    zipitem.path        = filename;
                    zipitem.rank        = length;
                    zipitem.name        = ary[length - 1];
                    zipitem.isdirectory = e.IsDirectory;
                    if (zipitem != null && !result.Contains(zipitem))
                    {
                        result.Add(zipitem);
                    }
                }
            }

            List <TreeNode> nodeList = TreeDiTui(result, 1);
            return(nodeList);
        }
コード例 #3
0
        /// <summary>
        /// 获取zip文件的目录树
        /// </summary>
        /// <param name="path">文件绝对路径</param>
        /// <param name="isExtract">是否解压zip包,默认不解压</param>
        /// <returns>目录树json</returns>
        public static List <TreeNode> ZipList(string path, bool isExtract = false)
        {
            List <ZipItem> result     = new List <ZipItem>();
            FileStream     fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); // 打开文件

            byte[] bytes = new byte[fileStream.Length];                                                       // 读取文件的 byte[]
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            Stream      ExistingZipFile = new MemoryStream(bytes);// 把 byte[] 转换成 Stream
            ReadOptions readoption      = new ReadOptions();

            readoption.Encoding = Encoding.GetEncoding("GB2312");
            //readoption.Encoding = Encoding.Default;
            using (ZipFile zip = ZipFile.Read(ExistingZipFile, readoption))
            {
                #region 判断是否解压文件夹
                if (isExtract)
                {
                    string folderpath = path.Replace(".zip", "");
                    new FileUtil().DelFolder(folderpath);
                    zip.ExtractAll(folderpath);
                    FileUtil.MoveFileAndFolderToFirstLayer(folderpath);
                    List <FileNode> list = FileUtil.GetDirectoryAndFilePath(folderpath);
                    foreach (var li in list)
                    {
                        ZipItem  zipitem  = new ZipItem();
                        string   filename = li.FileName;
                        string[] ary      = filename.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        int      length   = ary.Length;
                        zipitem.path        = filename;
                        zipitem.rank        = length;
                        zipitem.name        = ary[length - 1];
                        zipitem.isdirectory = li.IsDirectory;
                        if (zipitem != null && !result.Contains(zipitem))
                        {
                            result.Add(zipitem);
                        }
                    }
                }
                else
                {
                    foreach (ZipEntry e in zip)
                    {
                        ZipItem  zipitem  = new ZipItem();
                        string   filename = e.FileName;
                        string[] ary      = filename.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        int      length   = ary.Length;
                        zipitem.path        = filename;
                        zipitem.rank        = length;
                        zipitem.name        = ary[length - 1];
                        zipitem.isdirectory = e.IsDirectory;
                        if (zipitem != null && !result.Contains(zipitem))
                        {
                            result.Add(zipitem);
                        }
                    }
                }
                #endregion
            }
            List <TreeNode> nodeList = TreeDiTui(result, GetfirstDirectory(result, 1));
            return(nodeList);
        }