c# 实现zip打包

需求:将指定文件夹路径下的所有文件进行打包,并指定zip文件的输出路径

这里引用了一个程序集ICSharpCode.SharpZipLib.dll

实现:

先封装了基本的方法:记得先将该dll引用到当前工程中

dll下载地址:http://download.csdn.net/detail/dujiajiyiyi/9719720

若失效百度即可

public class ZipHelper
    {
        /// <summary>
        /// 打包指定文件夹下的文件,并在指定路径创建zip文件
        /// </summary>
        /// <param name="filesPath">指定待打包的文件夹</param>
        /// <param name="zipFilePath">创建的zip文件完全路径</param>
        /// <returns>是否成功生成</returns>
        public static bool CreateZipFile(string filesPath, string zipFilePath)
        {
            bool success = false;
            if (!Directory.Exists(filesPath))
            {
                Console.WriteLine("Cannot find directory '{0}'", filesPath);
                return false;
            }
            try
            {
                string[] filenames = Directory.GetFiles(filesPath);
                using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
                {

                    s.SetLevel(9); // 压缩级别 0-9
                    //s.Password = "123"; //Zip压缩文件密码
                    byte[] buffer = new byte[4096]; //缓冲区大小
                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    success = true;
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                success = false;
                Console.WriteLine("Exception during processing {0}", ex);
            }
            finally {
                
            }
            return success;
        }

        public  void UnZipFile( string zipFilePath)
        {
            if (!File.Exists(zipFilePath))
            {
                Console.WriteLine("Cannot find file '{0}'", zipFilePath);
                return;
            }

            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
            {

                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {

                    Console.WriteLine(theEntry.Name);

                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    // create directory
                    if (directoryName.Length > 0)
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(theEntry.Name))
                        {

                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    
    }


编写了一个测试函数:

public void ziptest(){
            ZipHelper helper = new ZipHelper();
            ZipHelper.CreateZipFile(@"E:\download\test", @"E:\download\test.zip");
        }


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,以下是一段用 C# 编写的代码,可以实现打包 SolidWorks 零件并自定义文件名: ```csharp using System; using System.IO; using System.Reflection; using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; namespace SolidWorksPacker { class Program { static void Main(string[] args) { // 初始化 SolidWorks 应用程序 SldWorks swApp = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as SldWorks; swApp.Visible = true; // 获取当前活动文档 ModelDoc2 swModel = swApp.ActiveDoc as ModelDoc2; if (swModel == null || swModel.GetType() != (int)swDocumentTypes_e.swDocPART) { Console.WriteLine("请先打开一个 SolidWorks 零件文件!"); return; } // 获取文件名和路径 string filePath = swModel.GetPathName(); string fileName = Path.GetFileNameWithoutExtension(filePath); string fileDir = Path.GetDirectoryName(filePath); // 另存为打包文件 string packedFilePath = Path.Combine(fileDir, $"{fileName}_packed.zip"); swApp.ActiveDoc.SaveAs3(packedFilePath, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, null, null); // 关闭当前文档 swApp.CloseDoc(swModel.GetTitle()); // 退出 SolidWorks 应用程序 swApp.ExitApp(); // 重命名打包文件 Console.WriteLine("请输入打包后的文件名:"); string newFileName = Console.ReadLine(); string newFilePath = Path.Combine(fileDir, $"{newFileName}.zip"); File.Move(packedFilePath, newFilePath); Console.WriteLine($"打包成功!文件已保存到 {newFilePath}。"); } } } ``` 运行此程序时,会提示用户输入打包后的文件名,并将打包后的文件重命名为用户输入的名称。 注意:此代码仅供参考,具体实现可能需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hustchenshu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值