在MFC中实现压缩及解压缩
lzw压缩、解压缩 C语言算法实现

将源文件拷贝到当前目录下,打开源程序lzw.cbp,在main.cpp中更改源文件、压缩文件、解压文件的名称、路径:#define S() (strcpy(sourfile, "sourfile.jpg")) //源文件名及路径#define C() (strcpy(codefile, "codefile.lzw")) //压缩文件名及路径#define D() (strcpy(destfile, "destfile.jpg")) //解压文件名及路径下面是具体实现,总共四个文件://头文件模块:lzw.h#ifndef LZW_H_INCLUDED#define LZW_H_INCLUDED#define N 90000#define M 100typedef unsigned int Uint;/*函数功能: 将sourfile文件用LZW压缩到codefile文件中函数参数:待压缩文件/压缩目标文件函数返回值:压缩成功(TRUE)/压缩失败(FALSE)*/void LzwEncoding(char sourfile[], char codefile[]);/*函数功能: 将codefile文件用LZW解压到destfilefile文件中函数参数:压缩目标文件/解压文件函数返回值:解压成功(TRUE)/解压失败(FALSE)*/void LzwDecoding(char codefile[], char destfile[]);/*函数功能:判断前缀数组与当前字符的结合是否在字典中存在索引函数参数:当前字符(cbuff)函数返回值:在字典中(TRUE)/不在字典中(F ALSE)*/bool IsFindDictKey(Uint buff);/*函数功能:判断前缀数组与当前字符的结合是否在字典中存在索引函数参数:前缀数组(preFix)/前缀数组的有效长度(preCount)/当前字符(cbuff)函数返回值:在字典中(TRUE)/不在字典中(F ALSE)*/bool IsDictValue(Uint preFix[], Uint preCount, Uint cbuff);/*函数功能:查找前缀数组在字典中的索引函数参数:前缀数组(preFix)/前缀数组的有效长度(preCount)函数返回值:前缀数组在字典中的索引*/Uint FindDictKey(Uint preFix[], Uint preCount);extern Uint dict[N][M]; //全局字典extern char sourfile[20]; //全局带压缩文件extern char codefile[20]; //全局压缩文件extern char destfile[20]; //全局解压文件#endif // LZW_H_INCLUDED运行源程序将执行压缩机解压缩两个过程:LzwEncoding(sourfile, codefile); //压缩文件sourfile到codefile LzwDecoding(codefile, destfile); //解压文件codefile到destfile//主函数模块:main.cpp#include <stdio.h>#include <stdlib.h>#include <string.h>#include "lzw.h"#define S() (strcpy(sourfile, "sourfile.pdf"))#define C() (strcpy(codefile, "codefile.lzw"))#define D() (strcpy(destfile, "destfile.pdf"))using namespace std;Uint dict[N][M]; //全局字典int main(void){char sourfile[20];char codefile[20];char destfile[20];S();C();D();LzwEncoding(sourfile, codefile);LzwDecoding(codefile, destfile);return 0;}//压缩模块:LzwEncoding.cpp#include <stdio.h>#include <stdlib.h>#include <windows.h>#include "lzw.h"using namespace std;/*函数功能:查找前缀数组在字典中的索引函数参数:前缀数组(preFix)/前缀数组的有效长度(preCount) 函数返回值:前缀数组在字典中的索引*/Uint DictKey(Uint preFix[], Uint preCount){Uint i;int flag = 0;for (i = 0; i < N; i++){Uint j = preCount;if (dict[i][0] == preCount){flag = 1;while ( j-- ){if (dict[i][j+1] != preFix[j]){flag = 0;break;}}}if ( flag ) break;}return i;}/*函数功能:判断前缀数组与当前字符的结合是否在字典中存在索引函数参数:前缀数组(preFix)/前缀数组的有效长度(preCount)/当前字符((int)sbuff[count]) 函数返回值:在字典中(TRUE)/不在字典中(F ALSE)*/bool IsDictValue(Uint preFix[], Uint preCount, Uint cbuff){int flag = 0;for (Uint i = 0; i < N; i++){if ((dict[i][0] == preCount+1) && (dict[i][preCount+1] == cbuff)){flag = 1;for (Uint j = 0; j < preCount; j++){if (dict[i][j+1] != preFix[j]){flag = 0;break;}}}if ( flag ) break;}if ( flag )return true;elsereturn false;}/*O(n*n)*//*函数功能: 将sourfile文件用LZW压缩到codefile文件中函数参数:待压缩文件/压缩目标文件函数返回值:压缩成功(TRUE)/压缩失败(FALSE)*/void LzwEncoding(char sourfile[], char codefile[]){FILE *sfp; //源文件句柄FILE *cfp; //编码文件句柄BYTE sbuff[N]; //源文件字节缓冲区Uint cbuff[N]; //压缩文件字节缓冲区Uint preCount; //前缀数组小标Uint preFix[N]; //前缀数组Uint dictCount = 255; //字典索引位置Uint cCount = 0; //压缩字节缓冲区下标Uint count = 0; //计数器Uint code; //临时编码if ((sfp = fopen(sourfile, "rb")) == NULL){printf("Read source file error!");exit( 0 );}if ((cfp = fopen(codefile, "wb")) == NULL){printf("Write code file error!");fclose(sfp);exit( 0 );}//初始化字典for (int i = 0; i < N; i++){if (i < 256){dict[i][0] = 1; //第i个索引处有一个字符dict[i][1] = i; //第i个索引处的字符}else{dict[i][0] = 0; //第i个索引处没有字符}}fseek(sfp, 0, SEEK_END);long fl = ftell(sfp); //获取原文件的字节数fseek(sfp, 0, SEEK_SET); //将文件指针移到文件开头处fread(sbuff, sizeof(sbuff[0]), fl, sfp); //将文件一次性读到缓冲区preCount = 0; //初始化前缀数组下标while ( fl-- ) //读取源文件内容并进行编码{if (IsDictValue(preFix, preCount, (int)sbuff[count])) //当前字符串在字典中preFix[preCount++] = (int)sbuff[count]; //将当前字符复制给前缀数组else{ //当前字符串不在字典中dictCount++; //字典增长//将前缀数组对应的索引写入编码文件code = DictKey(preFix, preCount);cbuff[cCount++] = code;//将前缀数组的字符及当前字符添加到字典中Uint icount = preCount; //记录前缀数组的有效长度dict[dictCount][0] = icount+1; //将dictCount索引处的字符数记录到字典while ( icount-- ){dict[dictCount][preCount-icount] = preFix[preCount-icount-1];}dict[dictCount][preCount-icount] = (int)sbuff[count];preCount = 0;preFix[preCount++] = (int)sbuff[count]; //将当前字符复制给前缀数组}count++;}code = DictKey(preFix, preCount);cbuff[cCount++] = code;fwrite(cbuff, sizeof(Uint), cCount, cfp); //将压缩文件整块写入文件fclose(sfp);fclose(cfp);}/* O(n^2*m) *///解压模块:LzwDecoding.cpp#include <stdio.h>#include <stdlib.h>#include <windows.h>#include "lzw.h"using namespace std;/*函数功能:判断buff是否为字典中的一个索引函数参数:无符号整形数buff函数返回值:是(TRUE)/否(FALSE)*/bool IsDictKey(Uint buff){if (dict[buff][0] == 0)return false;return true;}/*函数功能: 将codefile文件用LZW解压到destfilefile文件中函数参数:压缩目标文件/解压文件函数返回值:解压成功(TRUE)/解压失败(FALSE)*/void LzwDecoding(char codefile[], char destfile[]){FILE *cfp; //编码文件句柄FILE *dfp; //源文件句柄Uint oldCount; //old数组下标Uint oldCode[N] = {0}; //解码过的索引值??Uint cbuff[N/4]; //压缩文件缓冲区Uint cCount = 0; //压缩文件缓冲区下标BYTE dbuff[N] = {0}; //解压文件缓冲区Uint dCount = 0; //解压文件缓冲区下标Uint dictCount = 255; //字典长度Uint i, j; //循环变量if ((cfp = fopen(codefile, "rb")) == NULL){printf("Read coding file error!");exit( 0 );}if ((dfp = fopen(destfile, "wb")) == NULL){printf("Write decoding file error!");fclose(cfp);exit( 0 );}//初始化字典for (i = 0; i < N; i++){if (i < 256){dict[i][0] = 1; //第i个索引处有一个字符dict[i][1] = i; //第i个索引处的字符}else{dict[i][0] = 0; //第i个索引处没有字符}}fseek(cfp, 0, SEEK_END);long fl = ftell(cfp)/4; //获取原文件的编码数fseek(cfp, 0, SEEK_SET); //将文件指针移到文件开头处oldCount = 0; //初始化前缀数组下标、处理第一个编码fread(cbuff, sizeof(Uint), fl, cfp);//将压缩文件整块读入dbuff[dCount++]=cbuff[cCount];oldCode[oldCount++]=cbuff[cCount];fl--;while ( fl-- ) //读取源文件内容并进行编码{cCount++; //处理下一编码dictCount++; //字典增长if (IsDictKey(cbuff[cCount])) //字节在字典中{j = oldCount;dict[dictCount][0] = oldCount+1;while ( j-- ) //更新字典dict[dictCount][oldCount-j] = oldCode[oldCount-j-1];dict[dictCount][oldCount-j] = dict[cbuff[cCount]][1];i = dict[cbuff[cCount]][0];while ( i-- ) //将当前索引对应的字典值加入解压文件缓冲区dbuff[dCount++] = dict[cbuff[cCount]][dict[cbuff[cCount]][0]-i];//更新前缀数组oldCount = 0;i = dict[cbuff[cCount]][0];while ( i-- ){oldCode[oldCount]=dict[cbuff[cCount]][oldCount+1];oldCount++;}}else{i = oldCount;while ( i-- ) //前缀数组中的字典值加入解压文件缓冲区dbuff[dCount++]=oldCode[oldCount-i-1];dbuff[dCount++]=oldCode[0];dict[cbuff[cCount]][0] = oldCount+1;j = oldCount;while ( j-- ) //将前缀数组及前缀数组的第一个字符更新到字典dict[dictCount][oldCount-j+1] = oldCode[oldCount-j];dict[dictCount][oldCount-j]=oldCode[0];oldCode[oldCount++]=oldCode[0];}}fwrite(dbuff, sizeof(BYTE), dCount, dfp); //将解压文件缓冲区整块写入解压文件fclose(cfp);fclose(dfp);}改进方案:用结构体代替数组实现全局字典字典!其实现很简单,留给读者自行解决!。
在MFC中实现压缩及解压缩

在MFC中实现压缩及解压缩在MFC中实现Zip压缩及解压缩概述:This library allows creating, modifying, and extracting zip archives in a compatible way with PKZIP (2.5 and higher) and WinZip. Supported are all possible operations on the zip archive: creating, extracting, adding, deleting files from the archive, modifications of the existing archive. There is also support for creating and extracting multiple disk archives (on non-removable devices as well) and for password encryption and decryption. This module uses compression and decompression functions from the zlib library by Jean-loup Gailly and Mark Adler.这个库允许以兼容的方式创建、修改和提取以PKZIP(2.5或更高版本)和WinZip压缩的zip文档格式。
支持几乎所有zip文档格式的相关操作,比如:创建、提取、添加、以及从zip文档中删除文件,修改现有的zip文档等。
同时,该库也支持创建和提取分卷压缩的文档(非移动设备),并且支持带密码的加密以及解密操作。
本模块使用的压缩和解压缩函数来自于zlib库,作者:Jean-loup Gailly、Mark Adler.How to integrate with the projectZip is a static library and statically links to the compiled zlib.lib (version 1.13 nowadays). The zlib library can be replaced with a newer version, providing you also replace the files "zlib.h" and "zconf.h" in the Zip project. The Zip library uses MFC in a shared library as a Release and Debug configuration. Your project must use MFC in the same way in the appropriate project configuration. You may need to adapt this to your needs. To add Zip library functionality to your project, you need to link the library to the project. You can do this in at least two ways (in bothcases, you need to include the ZipArchive.h header in your sources like this: #include "ZipArchive.h"):如何将该库整合到您的工程中:Zip是个lib的静态库,他以静态链接的方式编译链接到zlib.lib库(现在是1.13版本)。
Windows CMD命令中的压缩与解压缩技巧

Windows CMD命令中的压缩与解压缩技巧在Windows操作系统中,CMD命令是一种非常强大的工具,它可以帮助我们完成各种各样的任务,包括文件的压缩与解压缩。
在本文中,我将为大家介绍一些常用的CMD命令,以及它们在文件压缩与解压缩方面的应用。
一、压缩文件1. 使用“compact”命令压缩文件在CMD命令行中,我们可以使用“compact”命令来压缩文件。
例如,如果我们想要压缩名为“example.txt”的文件,我们可以输入以下命令:compact /c example.txt这个命令将会压缩指定的文件,从而减小文件的大小。
压缩后的文件将会以“.cmp”作为扩展名。
2. 使用“makecab”命令压缩文件另一个常用的压缩命令是“makecab”。
与“compact”命令不同,它可以将多个文件压缩成一个cab文件。
例如,如果我们想要将名为“file1.txt”和“file2.txt”的文件压缩成一个名为“archive.cab”的文件,我们可以输入以下命令:makecab file1.txt file2.txt archive.cab这个命令将会创建一个包含指定文件的cab文件。
二、解压缩文件1. 使用“expand”命令解压缩文件在CMD命令行中,我们可以使用“expand”命令来解压缩文件。
例如,如果我们想要解压缩名为“example.cmp”的文件,我们可以输入以下命令:expand example.cmp这个命令将会解压缩指定的文件,从而恢复文件的原始大小和内容。
2. 使用“extrac32”命令解压缩文件另一个常用的解压缩命令是“extrac32”。
与“expand”命令不同,它可以解压缩cab文件。
例如,如果我们想要解压缩名为“archive.cab”的文件,我们可以输入以下命令:extrac32 archive.cab这个命令将会解压缩指定的cab文件,从而恢复其中包含的文件。
C#压缩或解压rar、zip文件方法实例

C#压缩或解压rar、zip⽂件⽅法实例前⾔为了便于⽂件在⽹络中的传输和保存,通常将⽂件进⾏压缩操作,常⽤的压缩格式有rar、zip和7z,本⽂将介绍在C#中如何对这⼏种类型的⽂件进⾏压缩和解压,并提供⼀些在C#中解压缩⽂件的开源库。
在C#.NET中压缩解压rar⽂件rar格式是⼀种具有专利⽂件的压缩格式,是⼀种商业压缩格式,不开源,对解码算法是公开的,但压缩算法是私有的,需要付费,如果需要在您的商业软件中使⽤rar格式进⾏解压缩,那么你需要为rar付费,rar在国内很流⾏是由于盗版的存在,正因为算法是不开源的,所以我们压缩rar并没有第三⽅的开源库可供选择,只能另寻出路。
针对rar的解压缩,我们通常使⽤winrar,⼏乎每台机器都安装了winrar,对于普通⽤户来说它提供基于⽤户界⾯的解压缩⽅式,另外,它也提供基于命令⾏的解压缩⽅式,这为我们在程序中解压缩rar格式提供了⼀个⼊⼝,我们可以在C#程序中调⽤rar的命令⾏程序实现解压缩,思路是这样的:1、判断注册表确认⽤户机器是否安装winrar程序,如果安装取回winrar安装⽬录。
2、创建⼀个命令⾏执⾏进程。
3、通过winrar的命令⾏参数实现解压缩。
⾸先我们通过下⾯的代码判断⽤户计算机是否安装了winrar压缩⼯具:如果已经安装winrar可通过如下代码返回winrar的安装位置,未安装则返回空字符串,最后并关闭注册表:public static string ExistsWinRar(){string result = string.Empty;string key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key);if (registryKey != null){result = registryKey.GetValue("").ToString();}registryKey.Close();return result;}/// <summary>/// 将格式为rar的压缩⽂件解压到指定的⽬录/// </summary>/// <param name="rarFileName">要解压rar⽂件的路径</param>/// <param name="saveDir">解压后要保存到的⽬录</param>public static void DeCompressRar(string rarFileName, string saveDir){string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(regKey);string winrarPath = registryKey.GetValue("").ToString();registryKey.Close();string winrarDir = System.IO.Path.GetDirectoryName(winrarPath);String commandOptions = string.Format("x {0} {1} -y", rarFileName, saveDir);ProcessStartInfo processStartInfo = new ProcessStartInfo();processStartInfo.FileName = bine(winrarDir, "rar.exe");processStartInfo.Arguments = commandOptions;processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;Process process = new Process();process.StartInfo = processStartInfo;process.Start();process.WaitForExit();process.Close();}/// <summary>/// 将⽬录和⽂件压缩为rar格式并保存到指定的⽬录/// </summary>/// <param name="soruceDir">要压缩的⽂件夹⽬录</param>/// <param name="rarFileName">压缩后的rar保存路径</param>public static void CompressRar(string soruceDir, string rarFileName){string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(regKey);string winrarPath = registryKey.GetValue("").ToString();registryKey.Close();string winrarDir = System.IO.Path.GetDirectoryName(winrarPath);String commandOptions = string.Format("a {0} {1} -r", rarFileName, soruceDir);ProcessStartInfo processStartInfo = new ProcessStartInfo();processStartInfo.FileName = bine(winrarDir, "rar.exe");processStartInfo.Arguments = commandOptions;processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;Process process = new Process();process.StartInfo = processStartInfo;process.Start();process.WaitForExit();process.Close();}在C#.NET中压缩解压zip⽂件zip是⼀种免费开源的压缩格式,windows平台⾃带zip压缩和解压⼯具,由于算法是开源的,所以基于zip的解压缩开源库也很多,SharpZipLib是⼀个很不错的C#库,它能够解压缩zip、gzip和tar格式的⽂件,⾸先下载SharpZipLib解压后,在您的项⽬中引⽤ICSharpCode.SharpZLib.dll程序集即可,下⾯是⼀些关于SharpZipLib压缩和解压的⽰例。
c#文件压缩zip或将zip文件解压的方法

c#⽂件压缩zip或将zip⽂件解压的⽅法1.必须Dll:ICSharpCode.SharpZipLib.dll。
可从Nutget程序包中获取。
2.压缩⽂件/// <summary>/// 压缩⽂件成zip/// </summary>/// <param name="fileZip">压缩成zip⽂件的绝对路径</param>/// <param name="fileName">被压缩指定⽂件的名字</param>/// <param name="zipFilePath"></param>/// <returns></returns>public bool CreateZipFile(string fileZip,string fileName, string zipFilePath){bool isZip = false;if (!Directory.Exists(zipFilePath)){($"Cannot find directory {zipFilePath}", false, "FileToZip");return isZip;}try{string[] filenames = Directory.GetFiles(zipFilePath);using (ZipOutputStream s = new ZipOutputStream(File.Create(fileZip))){s.SetLevel(9); // 压缩级别 0-9//s.Password = "123"; //Zip压缩⽂件密码byte[] buffer = new byte[4096]; //缓冲区⼤⼩foreach (string file in filenames.ToList()){if (file== zipFilePath+fileName)//指定被压缩⽂件的绝对路径{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);fs.Close();fs.Dispose();}break;}}s.Finish();s.Close();isZip = true;}}catch (Exception ex){($"Exception during processing {0}", false, "FileToZip");}return isZip;}3.将zip⽂件解压/// <summary>/// 解压⽂件/// </summary>/// <param name="zipFilePath">压缩⽂件的绝对路径</param>public void UnZipFile(string zipFilePath){if (!File.Exists(zipFilePath)){($"Cannot find file {zipFilePath}", false, "FileToZip");return;}using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))){ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName();string fileName = Path.GetFileName();// create directoryif (directoryName?.Length > 0){Directory.CreateDirectory(directoryName);}if (!string.IsNullOrEmpty(fileName)){using (FileStream streamWriter = File.Create()){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;}}}}}}}4.其它:其中的Logger是Log4的⽤法。
C#压缩或解压(rar和zip文件)

C#压缩或解压(rar和zip⽂件)/// <summary>/// 解压RAR和ZIP⽂件(需存在Winrar.exe(只要⾃⼰电脑上可以解压或压缩⽂件就存在Winrar.exe))/// </summary>/// <param name="UnPath">解压后⽂件保存⽬录</param>/// <param name="rarPathName">待解压⽂件存放绝对路径(包括⽂件名称)</param>/// <param name="IsCover">所解压的⽂件是否会覆盖已存在的⽂件(如果不覆盖,所解压出的⽂件和已存在的相同名称⽂件不会共同存在,只保留原已存在⽂件)</param>/// <param name="PassWord">解压密码(如果不需要密码则为空)</param>/// <returns>true(解压成功);false(解压失败)</returns>public static bool UnRarOrZip(string UnPath, string rarPathName, bool IsCover,string PassWord){if (!Directory.Exists(UnPath))Directory.CreateDirectory(UnPath);Process Process1 = new Process();Process1.StartInfo.FileName = "Winrar.exe";Process1.StartInfo.CreateNoWindow = true;string cmd = "";if (!string.IsNullOrEmpty(PassWord) && IsCover)//解压加密⽂件且覆盖已存在⽂件( -p密码 )cmd = string.Format(" x -p{0} -o+ {1} {2} -y", PassWord, rarPathName, UnPath);else if (!string.IsNullOrEmpty(PassWord) && !IsCover)//解压加密⽂件且不覆盖已存在⽂件( -p密码 )cmd = string.Format(" x -p{0} -o- {1} {2} -y", PassWord, rarPathName, UnPath);else if (IsCover)//覆盖命令( x -o+ 代表覆盖已存在的⽂件)cmd = string.Format(" x -o+ {0} {1} -y" , rarPathName,UnPath);else//不覆盖命令( x -o- 代表不覆盖已存在的⽂件)cmd = string.Format(" x -o- {0} {1} -y", rarPathName, UnPath);//命令Process1.StartInfo.Arguments = cmd;Process1.Start();Process1.WaitForExit();//⽆限期等待进程 winrar.exe 退出//Process1.ExitCode==0指正常执⾏,Process1.ExitCode==1则指不正常执⾏if (Process1.ExitCode == 0){Process1.Close();return true;}else{Process1.Close();return false;}}/// <summary>/// 压缩⽂件成RAR或ZIP⽂件(需存在Winrar.exe(只要⾃⼰电脑上可以解压或压缩⽂件就存在Winrar.exe))/// </summary>/// <param name="filesPath">将要压缩的⽂件夹或⽂件的绝对路径</param>/// <param name="rarPathName">压缩后的压缩⽂件保存绝对路径(包括⽂件名称)</param>/// <param name="IsCover">所压缩⽂件是否会覆盖已有的压缩⽂件(如果不覆盖,所压缩⽂件和已存在的相同名称的压缩⽂件不会共同存在,只保留原已存在压缩⽂件)</param> /// <param name="PassWord">压缩密码(如果不需要密码则为空)</param>/// <returns>true(压缩成功);false(压缩失败)</returns>public static bool CondenseRarOrZip(string filesPath, string rarPathName,bool IsCover, string PassWord){string rarPath = Path.GetDirectoryName(rarPathName);if (!Directory.Exists(rarPath))Directory.CreateDirectory(rarPath);Process Process1 = new Process();Process1.StartInfo.FileName = "Winrar.exe";Process1.StartInfo.CreateNoWindow = true;string cmd = "";if (!string.IsNullOrEmpty(PassWord) && IsCover)//压缩加密⽂件且覆盖已存在压缩⽂件( -p密码 -o+覆盖 )cmd = string.Format(" a -ep1 -p{0} -o+ {1} {2} -r", PassWord, rarPathName, filesPath);else if (!string.IsNullOrEmpty(PassWord) && !IsCover)//压缩加密⽂件且不覆盖已存在压缩⽂件( -p密码 -o-不覆盖 )cmd = string.Format(" a -ep1 -p{0} -o- {1} {2} -r", PassWord, rarPathName, filesPath);else if (string.IsNullOrEmpty(PassWord) && IsCover)//压缩且覆盖已存在压缩⽂件( -o+覆盖 )cmd = string.Format(" a -ep1 -o+ {0} {1} -r", rarPathName, filesPath);else//压缩且不覆盖已存在压缩⽂件( -o-不覆盖 )cmd = string.Format(" a -ep1 -o- {0} {1} -r", rarPathName, filesPath);//命令Process1.StartInfo.Arguments = cmd;Process1.Start();Process1.WaitForExit();//⽆限期等待进程 winrar.exe 退出//Process1.ExitCode==0指正常执⾏,Process1.ExitCode==1则指不正常执⾏if (Process1.ExitCode == 0){Process1.Close(); return true;}else{Process1.Close(); return false;}}。
编程语言中的压缩类型详解

编程语言中的压缩类型详解在当今数字化时代,数据的存储和传输变得越来越重要。
为了提高效率和节省空间,压缩技术成为了必不可少的一部分。
编程语言中的压缩类型是一种常见的技术,它可以在编程过程中对数据进行压缩和解压缩。
本文将详细介绍编程语言中常见的压缩类型及其应用。
一、无损压缩无损压缩是一种压缩技术,它可以将数据压缩成较小的文件大小,同时保持数据完整性。
在编程语言中,常见的无损压缩类型有以下几种:1. ZIP压缩:ZIP是一种广泛使用的压缩格式,它可以将多个文件和目录压缩成一个文件。
ZIP压缩可以减小文件的大小,方便存储和传输。
在编程中,我们可以使用各种编程语言提供的库来实现ZIP压缩和解压缩功能。
2. GZIP压缩:GZIP是一种常用的文件压缩格式,它使用DEFLATE算法对数据进行压缩。
GZIP压缩通常可以达到更高的压缩比,但压缩和解压缩的速度相对较慢。
在编程中,我们可以使用GZIP库来实现对文件或数据流的压缩和解压缩。
3. BZIP2压缩:BZIP2是一种高效的压缩算法,它可以在保持数据完整性的同时实现更高的压缩比。
BZIP2压缩在某些情况下可能比GZIP压缩更有效,但也会导致更长的压缩和解压缩时间。
在编程中,我们可以使用BZIP2库来实现对文件或数据流的压缩和解压缩。
二、有损压缩有损压缩是一种压缩技术,它可以将数据压缩成更小的文件大小,但会牺牲一定的数据质量。
在编程语言中,常见的有损压缩类型有以下几种:1. JPEG压缩:JPEG是一种常用的图像压缩格式,它可以将图像压缩成较小的文件大小。
JPEG压缩通过去除图像中的某些细节和颜色信息来实现较高的压缩比,但会导致图像质量的损失。
在编程中,我们可以使用各种图像处理库来实现JPEG压缩和解压缩功能。
2. MP3压缩:MP3是一种常用的音频压缩格式,它可以将音频文件压缩成较小的文件大小。
MP3压缩通过去除音频中的某些频率和声音细节来实现较高的压缩比,但会导致音频质量的损失。
Windows CMD命令中的文件压缩和解压技巧

Windows CMD命令中的文件压缩和解压技巧在Windows操作系统中,CMD命令是一种非常强大的工具,可以通过它来完成各种任务,包括文件压缩和解压。
本文将介绍一些常用的CMD命令,帮助读者更好地掌握文件压缩和解压技巧。
一、文件压缩技巧1. 使用“compress”命令压缩文件CMD命令中的“compress”命令可以将文件压缩为.cab格式。
使用该命令的语法为:compress -r <文件或文件夹路径> <目标路径>其中,“-r”参数表示递归压缩文件夹中的所有文件。
通过这个命令,我们可以将指定的文件或文件夹压缩为.cab格式的压缩包。
2. 使用“compact”命令压缩文件“compact”命令可以将文件或文件夹压缩为紧凑格式。
使用该命令的语法为:compact /c <文件或文件夹路径>这个命令将会把指定的文件或文件夹压缩为紧凑格式,从而节省磁盘空间。
如果需要解压缩文件,可以使用“compact /u”命令。
3. 使用“makecab”命令压缩文件“makecab”命令可以将文件或文件夹压缩为.cab格式。
使用该命令的语法为:makecab <文件或文件夹路径> <目标路径>通过这个命令,我们可以将指定的文件或文件夹压缩为.cab格式的压缩包。
与“compress”命令不同的是,“makecab”命令可以自定义压缩选项,例如指定压缩级别、文件排除等。
二、文件解压技巧1. 使用“expand”命令解压缩文件CMD命令中的“expand”命令可以解压缩.cab格式的压缩包。
使用该命令的语法为:expand <压缩包路径> <目标路径>通过这个命令,我们可以将指定的.cab格式压缩包解压缩到指定的目标路径。
需要注意的是,目标路径必须存在,否则解压缩操作将会失败。
2. 使用“compact”命令解压缩文件前面提到的“compact”命令不仅可以压缩文件,还可以解压缩文件。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
在MFC中实现Zip压缩及解压缩概述:This library allows creating, modifying, and extracting zip archives in a compatible way with PKZIP (2.5 and higher) and WinZip. Supported are all possible operations on the zip archive: creating, extracting, adding, deleting files from the archive, modifications of the existing archive. There is also support for creating and extracting multiple disk archives (on non-removable devices as well) and for password encryption and decryption. This module uses compression and decompression functions from the zlib library by Jean-loup Gailly and Mark Adler.这个库允许以兼容的方式创建、修改和提取以PKZIP(2.5或更高版本)和WinZip压缩的zip文档格式。
支持几乎所有zip文档格式的相关操作,比如:创建、提取、添加、以及从zip文档中删除文件,修改现有的zip文档等。
同时,该库也支持创建和提取分卷压缩的文档(非移动设备),并且支持带密码的加密以及解密操作。
本模块使用的压缩和解压缩函数来自于zlib库,作者:Jean-loup Gailly、Mark Adler.How to integrate with the projectZip is a static library and statically links to the compiled zlib.lib (version 1.13 nowadays). The zlib library can be replaced with a newer version, providing you also replace the files "zlib.h" and "zconf.h" in the Zip project. The Zip library uses MFC in a shared library as a Release and Debug configuration. Your project must use MFC in the same way in the appropriate project configuration. You may need to adapt this to your needs. To add Zip library functionality to your project, you need to link the library to the project. You can do this in at least two ways (in both cases, you need to include the ZipArchive.h header in your sources like this: #include "ZipArchive.h"):如何将该库整合到您的工程中:Zip是个lib的静态库,他以静态链接的方式编译链接到zlib.lib库(现在是1.13版本)。
当然工程中的zlib库能被替换成您认为当前的最新版本,同时“zlib.h”和“zconf.h”这两个文件别忘记也要做相应的替换。
在工程的Debug和Release编译时,Zip库通过MFC共享DLL的方式链接到MFC的运行库,所以,您的工程应该也是用同样的配置方式链接到MFC运行库才可以。
您可能会根据您的特定需求来做调整。
添加Zip库到您的工程中,之后链接到Zip库,您可以使用至少两种方式(在这两种方式中,您需要包含ZipArchive.h 这个头文件,例如:#include "ZipArchive.h")Method 1Add "..\Zip\debug(release)\ZipArchive.lib" to Project Settings->Link->Input->Object/library modules and add the Zip directory to the preprocessor searches (Project Settings -> C++ -> Preprocessor -> Additional include directories).Method 2 (simpler)Insert the Zip project into the workspace and set the project dependencies (your project dependent on the Zip project).方法1:将"..\Zip\debug(release)\ZipArchive.lib" 添加到您的工程设置中,具体设置选项的位置在(vc6为版本讲解):Project Settings->Link->Input->Object/library modules,同时还要添加到preprocessor searches,具体位置在:Project Settings -> C++ -> Preprocessor -> Additional include directorie备注:当然,也可以使用代码的方式实现方法1,代码为:#pragma comment(lib,“..\Zip\debug(release)\ZipArchive.lib”)方法2:将Zip工程插入到您当前的工作区(workspace)下,同时设定工程间的编译依赖关系,编译时您的工程要依赖Zip工程How to useThe details about using this library are in the sources. The example available for download at the bottom of the page is an almost complete compression\decompression program. There are only the main issues mentioned below about using this library. If you have a question about using the library and you can't find the answer here, don't hesitate to ask.如何使用?关于本库的详细使用信息见源代码。
示例代码已提供下载,里面包含了比较完全的压缩与解压缩程序。
Compression and decompressionThere are some functions defined for fast operations on archives; amongothers: AddNewFile(), ExtractFile(), DeleteFile(). You only need to call the functions Open() - before, and Close() - after using them. Remember to callthe Close() function when you finish working with the CZipArchive class.压缩与解压缩为了更快的实现压缩与解压缩操作,这里提供一些额外的函数定义,其中包括:AddNewFile(), ExtractFile(), DeleteFile(),您仅需要在使用之前调用Open()函数进行打开操作,在使用完成结束之后调用 Close()函数即可,当您使用完CZipArchive类之后一定要记得调用Close() 函数,切记!Multi-disk archivesThis library supports two kinds of multi-disk archives:1.Disk spanning performed in the compatible way with all other main zipprograms. It means that the archive can only be created on a removabledevice, the size of the volume is auto-detected, and the label is written to the disk. To use this kind of disk spanning, you need to define a static callback function for changing disks and set it withthe SetSpanCallback() function.2.Disk spanning performed in the internal mode, called in the sources TD spanmode. This allows creating multi disk archives also on non-removabledevices and with user-defined volume size. There is no need to set thecallback function in this mode.分卷压缩该库可支持两种方式的分卷压缩:1、。