相关文章推荐
卖萌的芹菜  ·  mysqld: can't create ...·  2 周前    · 
性感的橙子  ·  dlmopen ...·  1 月前    · 
八块腹肌的眼镜  ·  exec ...·  2 月前    · 
安静的饺子  ·  Pycharm中报错 No such ...·  3 月前    · 
开朗的烈酒  ·  vue.js - vuex: ...·  1 年前    · 
飞奔的铁板烧  ·  python -m http.server ...·  1 年前    · 
刚失恋的木瓜  ·  Airtest IDE ...·  1 年前    · 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using ICSharpCode.SharpZipLib.Zip;
  5. namespace TestConsole
  6. {
  7. internal class Program
  8. {
  9. private static void Main()
  10. {
  11. //CreateZipFile(@"d:\", @"d:\a.zip");
  12. UnZipFile(@ "E:\我的桌面.zip" );
  13. Console.Read();
  14. }
  15. /// <summary>
  16. ///     压缩文件为zip包
  17. /// </summary>
  18. /// <param name="filesPath"></param>
  19. /// <param name="zipFilePath"></param>
  20. private static bool CreateZipFile( string filesPath, string zipFilePath)
  21. {
  22. if (!Directory.Exists(filesPath))
  23. {
  24. return false ;
  25. }
  26. try
  27. {
  28. string [] filenames = Directory.GetFiles(filesPath);
  29. using (var s = new ZipOutputStream(File.Create(zipFilePath)))
  30. {
  31. s.SetLevel(9); // 压缩级别 0-9
  32. //s.Password = "123"; //Zip压缩文件密码
  33. var buffer = new byte [4096]; //缓冲区大小
  34. foreach ( string file in filenames)
  35. {
  36. var entry = new ZipEntry(Path.GetFileName(file));
  37. entry.DateTime = DateTime.Now;
  38. s.PutNextEntry(entry);
  39. using (FileStream fs = File.OpenRead(file))
  40. {
  41. int sourceBytes;
  42. do
  43. {
  44. sourceBytes = fs.Read(buffer, 0, buffer.Length);
  45. s.Write(buffer, 0, sourceBytes);
  46. } while (sourceBytes > 0);
  47. }
  48. }
  49. s.Finish();
  50. s.Close();
  51. }
  52. return true ;
  53. }
  54. catch (Exception ex)
  55. {
  56. Console.WriteLine( "Exception during processing {0}" , ex);
  57. }
  58. return false ;
  59. }
  60. /// <summary>
  61. ///     文件解压(zip格式)
  62. /// </summary>
  63. /// <param name="zipFilePath"></param>
  64. /// <returns></returns>
  65. private static List<FileInfo> UnZipFile( string zipFilePath)
  66. {
  67. var files = new List<FileInfo>();
  68. var zipFile = new FileInfo(zipFilePath);
  69. if (!File.Exists(zipFilePath))
  70. {
  71. return files;
  72. }
  73. using (var zipInputStream = new ZipInputStream(File.OpenRead(zipFilePath)))
  74. {
  75. ZipEntry theEntry;
  76. while ((theEntry = zipInputStream.GetNextEntry()) != null )
  77. {
  78. if (zipFilePath != null )
  79. {
  80. string dir = Path.GetDirectoryName(zipFilePath);
  81. if (dir != null )
  82. {
  83. string dirName = Path.Combine(dir, zipFile.Name.Replace(zipFile.Extension, "" ));
  84. string fileName = Path.GetFileName(theEntry.Name);
  85. if (! string .IsNullOrEmpty(dirName))
  86. {
  87. if (!Directory.Exists(dirName))
  88. {
  89. Directory.CreateDirectory(dirName);
  90. }
  91. }
  92. if (! string .IsNullOrEmpty(fileName))
  93. {
  94. string filePath = Path.Combine(dirName, theEntry.Name);
  95. using (FileStream streamWriter = File.Create(filePath))
  96. {
  97. var data = new byte [2048];
  98. while ( true )
  99. {
  100. int size = zipInputStream.Read(data, 0, data.Length);
  101. if (size > 0)
  102. {
  103. streamWriter.Write(data, 0, size);
  104. }
  105. else
  106. {
  107. break ;
  108. }
  109. }
  110. }
  111. files.Add( new FileInfo(filePath));
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return files;
  118. }
  119. }
  120. }
  1. /// <summary>
  2. ///     文件解压(Rar格式)
  3. /// </summary>
  4. /// <param name="rarFilePath"></param>
  5. /// <returns></returns>
  6. public static List<FileInfo> UnRarFile( string rarFilePath)
  7. {
  8. var files = new List<FileInfo>();
  9. var fileInput = new FileInfo(rarFilePath);
  10. if (fileInput.Directory != null )
  11. {
  12. string dirName = Path.Combine(fileInput.Directory.FullName,
  13. fileInput.Name.Replace(fileInput.Extension, "" ));
  14. if (! string .IsNullOrEmpty(dirName))
  15. {
  16. if (!Directory.Exists(dirName))
  17. {
  18. Directory.CreateDirectory(dirName);
  19. }
  20. }
  21. dirName = dirName.EndsWith( "\\") ? dirName : dirName + " \\"; //最后这个斜杠不能少!
  22. string shellArguments = string .Format( "x -o+ {0} {1}" , rarFilePath, dirName);
  23. using (var unrar = new Process())
  24. {
  25. unrar.StartInfo.FileName = @ "C:\Program Files\WinRAR\WinRAR.exe" ; //WinRar安装路径!
  26. unrar.StartInfo.Arguments = shellArguments; //隐藏rar本身的窗口
  27. unrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  28. unrar.Start();
  29. unrar.WaitForExit(); //等待解压完成
  30. unrar.Close();
  31. }
  32. var dir = new DirectoryInfo(dirName);
  33. files.AddRange(dir.GetFiles());
  34. }
  35. return files;
  36. }
  1. / <summary>
  2. / 文件解压2(rar格式)使用SharpCompress组件 需.net 3.5以上才支持!
  3. / </summary>
  4. / <param name="rarFilePath"></param>
  5. / <returns></returns>
  6. //private static List<FileInfo> UnRarFile(string rarFilePath)
  7. //{
  8. //    var files = new List<FileInfo>();
  9. //    if (File.Exists(rarFilePath))
  10. //    {
  11. //        var fileInput = new FileInfo(rarFilePath);
  12. //        using (Stream stream = File.OpenRead(rarFilePath))
  13. //        {
  14. //            var reader = ReaderFactory.Open(stream);
  15. //            if (fileInput.Directory != null)
  16. //            {
  17. //                string dirName = Path.Combine(fileInput.Directory.FullName, fileInput.Name.Replace(fileInput.Extension, ""));
  18. //                if (!string.IsNullOrEmpty(dirName))
  19. //                {
  20. //                    if (!Directory.Exists(dirName))
  21. //                    {
  22. //                        Directory.CreateDirectory(dirName);
  23. //                    }
  24. //                }
  25. //                while (reader.MoveToNextEntry())
  26. //                {
  27. //                    if (!reader.Entry.IsDirectory)
  28. //                    {
  29. //                        reader.WriteEntryToDirectory(dirName, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
  30. //                        files.Add(new FileInfo(reader.Entry.FilePath));
  31. //                    }
  32. //                }
  33. //            }
  34. //        }
  35. //    }
  36. //    return files;
  37. //}
  38. /// <summary>
    /// 解压功能(解压压缩文件到指定目录)
    /// </summary>
    /// <param name="fileToUnZip">待解压的文件</param>
    /// <param name="zipedFolder">指定解压目标目录</param>
    /// <param name="password">密码</param>
    /// <returns>解压结果</returns>
    public static bool ChargeUnZip(string fileToUnZip, string zipedFolder, string password)
    {
    bool result = true;
    FileStream fs = null;
    ZipInputStream zipStream = null;
    ZipEntry ent = null;
    string fileName;

    if (!File.Exists(fileToUnZip))
    return false;

    if (!Directory.Exists(zipedFolder))
    Directory.CreateDirectory(zipedFolder);

    try
    {
    zipStream = new ZipInputStream(File.OpenRead(fileToUnZip));
    if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
    while ((ent = zipStream.GetNextEntry()) != null)
    {
    if (!string.IsNullOrEmpty(ent.Name))
    {
    fileName = Path.Combine(zipedFolder, ent.Name);
    fileName = fileName.Replace('/', '\\');//change by Mr.HopeGi

    if (fileName.EndsWith("\\"))
    {
    Directory.CreateDirectory(fileName);
    continue;
    }
    fs= new FileStream(fileName, FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite);
    int size = 2048;
    byte[] data = new byte[size];

    size = zipStream.Read(data, 0, data.Length);
    if (size > 0)
    {
    fs.Write(data, 0, size);
    fs.Flush();
    }
    else
    break;
    }
    }
    }
    catch
    {
    result = false;
    }
    finally
    {
    if (fs != null)
    {
    fs.Close();
    fs.Dispose();
    }
    if (zipStream != null)
    {
    zipStream.Close();
    zipStream.Dispose();
    }
    if (ent != null)
    {
    ent = null;
    }
    fs.Flush();
    fs.Close();

    GC.Collect();
    GC.Collect(1);
    }
    return result;
    }

    /// <summary>
    /// 解压功能(解压压缩文件到指定目录)
    /// </summary>
    /// <param name="fileToUnZip">待解压的文件</param>
    /// <param name="zipedFolder">指定解压目标目录</param>
    /// <returns>解压结果</returns>
    public static bool ChargeUnZip1(string fileToUnZip, string zipedFolder)
    {
    bool result = ChargeUnZip(fileToUnZip, zipedFolder, null);
    return result;
    }
using System;  using System.Collections.Generic;  using System.IO;  using ICSharpCode.SharpZipLib.Zip;    namespace TestConsole  {      internal class Program      {          private
已知问题:不支持空 文件 压缩 支持:1.多重 文件 压缩 2. 决不用从根目录 压缩 ,即如果 压缩 的是C:\a\b\c\d , 压缩 成C:\x.zip,打开看到的是d\....而非a\b\c\d\... 本代码基于xjzdr的代码上 修改 ,主要 修改 压缩 部分的代码
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:button ID="Button1" runat="server" text="上传" OnClick="Button1_Click" /> </form&g... 你要明白,任何问题都不是孤立存在的,一定有人曾经遇到过,并且已经有更好的解决办法了,只是我还不知道。我不应该在黑暗中独自前行,去重新发明轮子,也许我的顿悟,只是别人的基本功!我应该要站在巨人的肩膀上,学习更成熟的经验和方法,然后再来解决这个问题 11-01
使用软件:Visual Studio 2019、Microsoft SQL Server Management Studio 18最近期末大作业,整理的代码。1.退出系统: MessageBoxButtons mess = MessageBoxButtons.OKCancel; DialogResult dr = MessageBox.Show("确定要退出系统吗?", "提示", mess); if (dr == DialogResult.OK)
调用方法: string zipUrl = @"C:\Users\Administrator\Desktop\test.zip"; AppendToZip.Main(zipUrl); 文件 只是一个案例。添加多个可以循环以下代码: zipStream.PutNextEntry(newEntry); StreamUtils.Copy(streamReader, zipStream, buffer); zipStream.CloseEntry(); RAR格式还未测试
using System.IO.Compression; public void CompressFile(string sourceFilePath, string compressedFilePath) // 创建一个 文件 流来读取源 文件 using (FileStream sourceStream = new FileStream(sourceFilePath, FileMode.Open)) // 创建一个 文件 流来写入 压缩 后的 文件 using (FileStream compressedStream = File.Create(compressedFilePath)) // 创建一个 压缩 流 using (GZipStream compressionStream = new GZipStream(compressedStream, CompressionMode.Compress)) // 将源 文件 复制到 压缩 流中 sourceStream.CopyTo(compressionStream); 以上代码将源 文件 压缩 为 GZip 格式,并将 压缩 后的 文件 写入到指定的目标路径中。你可以根据需要 修改 代码以支持其他 压缩 格式。