压缩解压缩文件(zip格式)
作者:佚名
上传时间:2019-05-31
版权申诉
using System; using System.Collections.Generic; using System.IO; using ICSharpCode.SharpZipLib.Zip; namespace TestConsole { internal class Program { private static void Main() { //CreateZipFile(@"d:\", @"d:\a.zip"); UnZipFile(@"E:\我的桌面.zip"); Console.Read(); } ////// 压缩文件为zip包 /// /// /// private static bool CreateZipFile(string filesPath, string zipFilePath) { if (!Directory.Exists(filesPath)) { return false; } try { string[] filenames = Directory.GetFiles(filesPath); using (var s = new ZipOutputStream(File.Create(zipFilePath))) { s.SetLevel(9); // 压缩级别 0-9 //s.Password = "123"; //Zip压缩文件密码 var buffer = new byte[4096]; //缓冲区大小 foreach (string file in filenames) { var 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); } } s.Finish(); s.Close(); } return true; } catch (Exception ex) { Console.WriteLine("Exception during processing {0}", ex); } return false; } ////// 文件解压(zip格式) /// /// ///private static List UnZipFile(string zipFilePath) { var files = new List (); var zipFile = new FileInfo(zipFilePath); if (!File.Exists(zipFilePath)) { return files; } using (var zipInputStream = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = zipInputStream.GetNextEntry()) != null) { if (zipFilePath != null) { string dir = Path.GetDirectoryName(zipFilePath); if (dir != null) { string dirName = Path.Combine(dir, zipFile.Name.Replace(zipFile.Extension, "")); string fileName = Path.GetFileName(theEntry.Name); if (!string.IsNullOrEmpty(dirName)) { if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } } if (!string.IsNullOrEmpty(fileName)) { string filePath = Path.Combine(dirName, theEntry.Name); using (FileStream streamWriter = File.Create(filePath)) { var data = new byte[2048]; while (true) { int size = zipInputStream.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } files.Add(new FileInfo(filePath)); } } } } } return files; } } }
////// 文件解压(Rar格式) /// /// ///public static List UnRarFile(string rarFilePath) { var files = new List (); var fileInput = new FileInfo(rarFilePath); if (fileInput.Directory != null) { string dirName = Path.Combine(fileInput.Directory.FullName, fileInput.Name.Replace(fileInput.Extension, "")); if (!string.IsNullOrEmpty(dirName)) { if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } } dirName = dirName.EndsWith("\\") ? dirName : dirName + "\\"; //最后这个斜杠不能少! string shellArguments = string.Format("x -o+ {0} {1}", rarFilePath, dirName); using (var unrar = new Process()) { unrar.StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe"; //WinRar安装路径! unrar.StartInfo.Arguments = shellArguments; //隐藏rar本身的窗口 unrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; unrar.Start(); unrar.WaitForExit(); //等待解压完成 unrar.Close(); } var dir = new DirectoryInfo(dirName); files.AddRange(dir.GetFiles()); } return files; }
////////// 文件解压2(rar格式)使用SharpCompress组件 需.net 3.5以上才支持! ///// ///// ///////private static List UnRarFile(string rarFilePath) //{ // var files = new List (); // if (File.Exists(rarFilePath)) // { // var fileInput = new FileInfo(rarFilePath); // using (Stream stream = File.OpenRead(rarFilePath)) // { // var reader = ReaderFactory.Open(stream); // if (fileInput.Directory != null) // { // string dirName = Path.Combine(fileInput.Directory.FullName, fileInput.Name.Replace(fileInput.Extension, "")); // if (!string.IsNullOrEmpty(dirName)) // { // if (!Directory.Exists(dirName)) // { // Directory.CreateDirectory(dirName); // } // } // while (reader.MoveToNextEntry()) // { // if (!reader.Entry.IsDirectory) // { // reader.WriteEntryToDirectory(dirName, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); // files.Add(new FileInfo(reader.Entry.FilePath)); // } // } // } // } // } // return files; //}
免责申明:文章和图片全部来源于公开网络,如有侵权,请通知删除 server@dude6.com
用户评论
相关推荐
压缩解压缩文件(zip格式)
using System;using System.Collections.Generic;using System.IO;using ICSharpCode.SharpZipLib.Zip
ZIP包格式压缩解压缩
简单易用的ZIP包格式压缩/解压缩源码文件,仅支持传统加密/解压方式。主类:CZipImplement,接口Zip_PackFiles、Zip_UnPackFiles、Zip_GetPackData。
RAR
79KB
2020-12-09 19:36
Android Zip文件解压缩代码
在Android平台中如何实现Zip文件的解压缩功能呢? 因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面Android123给大家一个解压缩zi
Java如何压缩和解压缩zip文件
本示例演示如何使用Java进行zip文件的压缩和解压缩,利用JDK自带的java.util.zip.ZipInputStream和java.util.zip.ZipOutputStream实现文件压缩
JDK 1.8
IntelliJ IDEA
2023-03-23 00:35
java压缩和解压缩Zip、Jar、Gzip文件
我们经常会使用WinZIP等压缩软件将文件进行压缩以方便传输。在java里面也提供了将文件进行压缩以减少传输时的数据量的类,可以很方便的将文件压缩成ZIP、JAR、GZIP等形式,G
iOS开发之解压缩zip文件
转载自: http://blog.sina.com.cn/s/blog_833996210100udkl.html 从
Java如何批量解压缩zip文件
本示例代码演示了Java如何使用ZipInputStream和ZipEntry类批量解压缩zip文件,使用了Java提供的IO流以及文件操作类,可以用于快速解压缩多个zip文件。import jav
Java 8
IntelliJ IDEA
2023-03-14 20:38
java.util.zip解压缩文件ZIP格式压缩文件.rar
Java解压缩文件,并以ZIP格式压缩文件,主要是使用java.util.zip包中的类来实现解压、压缩文件功能,如果你对这个类并不太熟悉,你正好可以参考一下这个类是如何用的。
RAR
0B
2020-05-04 16:46
tif格式解压缩文件
里面是JPEG压缩格式的tif文件
RAR
0B
2019-06-04 04:22
zip压缩解压缩文件
zip压缩/解压缩文件
RAR
0B
2019-09-10 09:30