想实现那种像winrar制作的自解压exe文件那样
可以在没有winrar的环境下运行
运行时将压缩包解压,解压完毕可以自动运行某个文件。现在用vb.net实现了解压和运行的功能,但bin\debug下是一堆
data\xxx.zip,xxx.exe,xxx.config的文件,能否把它们搞成一个exe文件,运行起来和运行xxx.exe一样的效果希望大家谈谈自己的看法
可以在没有winrar的环境下运行
运行时将压缩包解压,解压完毕可以自动运行某个文件。现在用vb.net实现了解压和运行的功能,但bin\debug下是一堆
data\xxx.zip,xxx.exe,xxx.config的文件,能否把它们搞成一个exe文件,运行起来和运行xxx.exe一样的效果希望大家谈谈自己的看法
比如 exe 文件是 1000 字节
压缩包是 1000 字节
哪么最终就是 2000 字节exe 文件后面设置一个起始为如 FF 00 FF CC XX
后面的就是压缩文件;exe 运行后读取自己,找到 FF 00 FF CC XX 后
后面的就都是压缩包了,然后借用运行里面的东西原理就是这样或者用资源文件更简单一些(msdn上有列子)
解压算法网上有公开的
如果你不想很头疼的话
就用SharpZipLib.dll
这个很方便把你要解压的文件放到你说的xx.exe中就可以了using System;
using System.IO;using ICSharpCode.SharpZipLib.Zip;class MainClass
{
public static void Main(string[] args)
{
// Perform some simple parameter checking. More could be done
// like checking the target file name is ok, disk space, and lots
// of other things, but for a demo this covers some obvious traps.
if ( args.Length < 2 ) {
Console.WriteLine("Usage: CreateZipFile Path ZipFile");
return;
} if ( !Directory.Exists(args[0]) ) {
Console.WriteLine("Cannot find directory '{0}'", args[0]);
return;
} try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] filenames = Directory.GetFiles(args[0]);
// 'using' statements gaurantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream s = new ZipOutputStream(File.Create(args[1]))) {
s.SetLevel(9); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
foreach (string file in filenames) {
// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
// Setup the entry data as required.
// Crc and size are handled by the library for seakable streams
// so no need to do them here. // Could also use the last write time or similar for the file.
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using ( FileStream fs = File.OpenRead(file) ) {
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do {
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while ( sourceBytes > 0 );
}
}
// Finish/Close arent needed strictly as the using statement does this automatically
// Finish is important to ensure trailing information for a Zip file is appended. Without this
// the created file would be invalid.
s.Finish();
// Close is important to wrap things up and unlock the file.
s.Close();
}
}
catch(Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
// No need to rethrow the exception as for our purposes its handled.
}
}
}