Example #1
0
        /// <summary>
        /// 压缩所有文件files,为压缩文件zipFile, 以相对于BaseDir的路径构建压缩文件子目录,ignoreNames指定要忽略的文件或目录
        /// </summary>
        public static bool zip(String zipPath, String BaseDir, String[] files, String Password = null, String[] ignoreNames = null)
        {
            if (files == null || files.Length == 0)
            {
                return(false);
            }
            if (zipPath == null || zipPath.Equals(""))
            {
                zipPath = FileTools.getPathNoExt(files[0]) + ".zip";                                            // 默认以第一个文件命名压缩文件
            }
            if (BaseDir == null || BaseDir.Equals(""))
            {
                BaseDir = FileTools.getParent(files[0]);                                                                // 默认以第一个文件的父目录作为基础路径
            }
            Console.WriteLine("所有待压缩文件根目录:" + BaseDir);

            try
            {
                FileTools.mkdirs(FileTools.getParent(zipPath));         // 创建目标路径
                Console.WriteLine("创建压缩文件:" + zipPath);

                FileStream      input     = null;
                ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipPath));
                if (Password != null && !Password.Equals(""))
                {
                    zipStream.Password = Password;
                }

                files = FileTools.getSubFiles(files);               // 获取子目录下所有文件信息
                for (int i = 0; i < files.Length; i++)
                {
                    if (ContainsIgnoreName(files[i], ignoreNames))
                    {
                        continue;                                               // 跳过忽略的文件或目录
                    }
                    String entryName = FileTools.relativePath(BaseDir, files[i]);
                    zipStream.PutNextEntry(new ZipEntry(entryName));
                    Console.WriteLine("添加压缩文件:" + entryName);

                    if (File.Exists(files[i]))                  // 读取文件内容
                    {
                        input = File.OpenRead(files[i]);
                        Random rand   = new Random();
                        byte[] buffer = new byte[10240];
                        int    read   = 0;
                        while ((read = input.Read(buffer, 0, 10240)) > 0)
                        {
                            zipStream.Write(buffer, 0, read);
                        }
                        input.Close();
                    }
                }
                zipStream.Close();
                Console.WriteLine("文件压缩完成!");

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// 解压文件 到指定的路径,可通过targeFileNames指定解压特定的文件
        /// </summary>
        public static bool unzip(String zipPath, String targetPath = null, String Password = null, String[] targeFileNames = null)
        {
            if (File.Exists(zipPath))
            {
                if (targetPath == null || targetPath.Equals(""))
                {
                    targetPath = FileTools.getPathNoExt(zipPath);
                }
                Console.WriteLine("解压文件:" + zipPath);
                Console.WriteLine("解压至目录:" + targetPath);

                try
                {
                    ZipInputStream zipStream = null;
                    FileStream     bos       = null;

                    zipStream = new ZipInputStream(File.OpenRead(zipPath));
                    if (Password != null && !Password.Equals(""))
                    {
                        zipStream.Password = Password;
                    }

                    ZipEntry entry = null;
                    while ((entry = zipStream.GetNextEntry()) != null)
                    {
                        if (targeFileNames != null && targeFileNames.Length > 0)                // 若指定了目标解压文件
                        {
                            if (!ContainsIgnoreName(entry.Name, targeFileNames))
                            {
                                continue;                                                       // 跳过非指定的文件
                            }
                        }

                        String target = targetPath + "\\" + entry.Name;
                        if (entry.IsDirectory)
                        {
                            FileTools.mkdirs(target);                    // 创建目标路径
                        }
                        if (entry.IsFile)
                        {
                            FileTools.mkdirs(FileTools.getParent(target));

                            bos = File.Create(target);
                            Console.WriteLine("解压生成文件:" + target);

                            int    read   = 0;
                            byte[] buffer = new byte[10240];
                            while ((read = zipStream.Read(buffer, 0, 10240)) > 0)
                            {
                                bos.Write(buffer, 0, read);
                            }
                            bos.Flush();
                            bos.Close();
                        }
                    }
                    zipStream.CloseEntry();

                    Console.WriteLine("解压完成!");
                    return(true);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());;
                }
            }
            return(false);
        }