⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unrlezip.cpp

📁 自己写的RLEZIP
💻 CPP
字号:
#include <fstream>
#include <iostream>
#include <windows.h>
#include <direct.h>
#include <string>

using namespace std;
ofstream fout;
ifstream fin;

int cmp(unsigned char *p1, unsigned char *p2)
{
	while(*p1++ == *p2++);
	return (*p2 == 0);
}

unsigned char *unrlezip_file(unsigned char *in, unsigned int *size)
{
	unsigned char marker, symbol;
	_int64 i, count, inpos, insize = 0;
	unsigned char *name = new unsigned char[50], *np = name;
	memset(name, 0, 50);
	while(*in++ != '+');
	while(*np++ = *in++) if (*in == ']') break;
	in++;
	fout.open((char *)name, ios::out|ios::binary);
	inpos = 0; marker = *in++; (*size)++;
	while(!cmp(in, (unsigned char *)"[/ff]"))
	{
		symbol = *in++;
		(*size)++;
		if(symbol == marker)
		{
			count = *in++;
			(*size)++;
			if(count <= 2)
				for(i = 0; i <= count; i++)
					fout.put(marker);
			else
			{
				if(count & 0x80)
				{
					count = ((count & 0x7f) << 8) + *in++;
					(*size)++;
				}
				symbol = *in++;
				(*size)++;
				for(i = 0; i <= count; i++)
					fout.put(symbol);
			}
		}
		else
			fout.put(symbol);
	}
	fout.close();
	(*size) += 5;
	in = in + 5;
	return in;
}

unsigned char *unrlezip_dir(unsigned char *in, unsigned int *size)
{
	unsigned char *name = new unsigned char[50], *np = name;
	unsigned int insize = 0;
	memset(name, 0, 50);
	while(*in++ != '+') insize++;
	while(*np++ = *in++) 
	{
		insize++;
		if (*in == ']') break;
	}
	in++;
	_mkdir((char *)name); _chdir((char *)name);
	while(1)
	{
		if (cmp(in, (unsigned char *)"[ff+"))
			in = unrlezip_file(in, size);
		else if(cmp(in, (unsigned char *)"[rr+"))
			in = unrlezip_dir(in, size);
		else if (cmp(in, (unsigned char *)"[/rr]"))
		{
			in = in + 5;
			*size +=insize;
			_chdir("../");
			return in;
		}
		else
		{
			insize++; in++;
		}
	}
}

int main(int arg, char *argv[])
{
	char *source = argv[1];
	unsigned char *in, *p1, *p2;
	unsigned int insize, ps1, ps2, tsize;
	fin.open(source, ios::in|ios::binary);
	if (!fin)
	{
		cout <<"文件错误!" <<endl;
		return 0;
	}
   	fin.seekg (0, ios::end);
	insize = fin.tellg(); 
	fin.seekg (0, ios::beg);
	in = new unsigned char[insize];
	fin.read((char *)in, insize);
	fin.close();
	tsize = 0;
	do
	{
		p1 = in; p2 = in; 
		ps1 = tsize;
		while(!cmp(p1, (unsigned char *)"[rr+") && ps1 < insize - 10) 
		{
			p1++; ps1++;
		}
		if (ps1 >= insize - 10) p1 = NULL;
		ps2 = tsize;
		while(!cmp(p2, (unsigned char *) "[ff+")  && ps2 < insize - 10)
		{
			p2++;ps2++;
		}
		if (ps2 >= insize - 10)	p2 = NULL;
		if ((!p1 && p2 ) || (p2 && p1 > p2))
		{
			in = unrlezip_file(p2, &ps2);
			tsize = ps2;
		}
		else if ((p1 && !p2 ) || (p1 && p2 > p1))
		{
			in = unrlezip_dir(p1, &ps1);
			tsize = ps1;
		}
	} while (p1 || p2);
	cout <<"完成!" <<endl;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -