• 大小: 346KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: C#
  • 标签: 压缩  

资源简介

程序实现压缩与解压缩 zip 程序实现压缩与解压缩 压缩

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;

using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Checksums;

namespace CreateZip
{
    class Program
    {
        static void Main(string[] args)
        {
           string dir=Environment.CurrentDirectory;
           dir = dir + “\\UpLoad\\“;
           try
           {
               AddFolder(dir);
               Console.WriteLine(“Successfully“);
           }
           catch(Exception e)
           {
               Console.WriteLine(e.Message);
           }
           Console.ReadLine();
        }
        static void AddFolder(string Dir)
        {
            DirectoryInfo di = new DirectoryInfo(Dir);
            foreach (FileInfo fi in di.GetFiles())
            {
                string zipname = fi.Name.Substring(0 6);
                zipname = fi.DirectoryName+“\\“ + zipname + “.zip“;
                string[] FileProperties = new string[3];
                FileProperties[0] = fi.FullName;
                
                //压缩后的目标文件
                //FileProperties[1] = destPath + “\\“ + System.IO.Path.GetFileNameWithoutExtension(fullName) + “.zip“;
                FileProperties[1] = zipname;
                FileProperties[2] = fi.Name;
                ZipFileMain(FileProperties);
                fi.Delete();
            }
        }

        static void ZipFileMain(string[] args)
        {
            string[] filenames = new string[] { args[0] };
            string name = args[2];
            if (System.IO.File.Exists(args[1]))
            {
                Add(new string[] { args[1] args[0] args[2] });
                return;
            }
            Crc32 crc = new Crc32();

            FileStream filestream = new FileStream(args[1] FileMode.OpenOrCreate);
            ZipOutputStream s = new ZipOutputStream(filestream);

            s.SetLevel(6);

            foreach (string file in filenames)
            {
                //打开压缩文件
                FileStream fs = File.OpenRead(file);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer 0 buffer.Length);
                ZipEntry entry = new ZipEntry(name);

                entry.DateTime = DateTime.Now;
                entry.Size = fs.Length;
                fs.Close();

                crc.Reset();
                crc.Update(buffer);

                entry.Crc = crc.Value;
                s.PutNextEntry(entry);
                s.Write(buffer 0 buffer.Length);

            }
            s.Finish();
            s.Close();
        }

        static void Add(string[] fileSpecs)
        {
            string zipFileName = fileSpecs[0];
            using (ZipFile zipFile = new ZipFile(zipFileName))
            {
                zipFile.BeginUpdate();

                zipFile.Add(fileSpecs[1] fileSpecs[2]);

          

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2010-11-17 18:00  CreateZip\
     目录           0  2010-11-17 18:00  CreateZip\bin\
     目录           0  2010-11-17 18:00  CreateZip\bin\Debug\
     文件       16384  2010-11-17 11:08  CreateZip\bin\Debug\CreateZip.exe
     文件       13824  2010-11-17 11:08  CreateZip\bin\Debug\CreateZip.pdb
     文件        5632  2005-12-08 14:51  CreateZip\bin\Debug\CreateZip.vshost.exe
     文件      200704  2010-05-25 12:55  CreateZip\bin\Debug\ICSharpCode.SharpZipLib.dll
     目录           0  2011-03-31 11:40  CreateZip\bin\Debug\UpLoad\
     目录           0  2011-03-31 11:40  CreateZip\bin\Debug\UpLoad\43.doc\
     文件       45056  2010-11-17 11:04  CreateZip\bin\Debug\UpLoad\43.doc\43.doc
     目录           0  2010-11-17 18:00  CreateZip\bin\Debug\UpLoad\temp\
     文件      167936  2010-11-17 09:21  CreateZip\bin\Debug\UpLoad\temp\101117092040324.xls
     文件       50688  2010-11-17 09:21  CreateZip\bin\Debug\UpLoad\temp\101117092045081.xls
     文件      167936  2010-11-17 09:21  CreateZip\bin\Debug\UpLoad\temp\101117092049878.xls
     文件      167936  2010-11-17 09:21  CreateZip\bin\Debug\UpLoad\temp\101117092055296.xls
     文件      167936  2010-11-17 09:38  CreateZip\bin\Debug\UpLoad\temp\101117093817740.xls
     文件      167936  2010-11-17 09:48  CreateZip\bin\Debug\UpLoad\temp\101117094817545.xls
     文件        2244  2010-11-17 10:39  CreateZip\CreateZip.csproj
     目录           0  2010-11-17 18:00  CreateZip\obj\
     文件         933  2011-03-31 11:35  CreateZip\obj\CreateZip.csproj.FileListAbsolute.txt
     目录           0  2010-11-17 18:00  CreateZip\obj\Debug\
     文件       16384  2010-11-17 11:08  CreateZip\obj\Debug\CreateZip.exe
     文件       13824  2010-11-17 11:08  CreateZip\obj\Debug\CreateZip.pdb
     目录           0  2010-11-17 18:00  CreateZip\obj\Debug\Refactor\
     文件        2828  2010-11-17 18:00  CreateZip\obj\Debug\ResolveAssemblyReference.cache
     目录           0  2010-11-17 18:00  CreateZip\obj\Debug\TempPE\
     文件        3081  2010-11-17 11:08  CreateZip\Program.cs
     目录           0  2010-11-17 18:00  CreateZip\Properties\
     文件        1227  2010-11-17 10:16  CreateZip\Properties\AssemblyInfo.cs

评论

共有 条评论