📄 lzo.cpp
字号:
// lzo.cpp : Defines the entry point for the console application.
//
#pragma warning(disable :4786)
#include "stdafx.h"
#include "windows.h"
#include <stdlib.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
#include "lzo.h"
#include "estdio.h"
#include "ufind.h"
#include "pack.h"
#include "direct.h"
/* This function support 2 method of compress : LZO or Zlib
* LZO 's code size is much small, decompress speed is fast(3 times slow that memcopy), but compress rate is low
* Zlib's bigger, seemed ok , very high
* To use LZO, just turn on the macro USE_LZO
*/
#define USE_LZO 0
#if USE_LZO
#include "lzo1x.h"
#else
#include "zlib.h"
#endif
struct lzo{
int magic ; // 'lzod'
int fileLen; //filelen
int uncompLen;
int compLen;
char comp[0];
};
#define LZO1X_999_MEM_COMPRESS ((14*16384L* sizeof(short)))
static char workmem[LZO1X_999_MEM_COMPRESS];
#define M_MAX_BUFSIZE Max_Comp_Len
void lzoCompress(const char *name)
{
#define Max_Comp_Len (1024*1024)
static char comp[Max_Comp_Len];
unsigned int comp_len, uncomp_len;
long len;
char *buff = BinRead(name,(long *)&uncomp_len);
#if USE_LZO
lzo1x_999_compress ((const unsigned char*)( buff), uncomp_len,
(unsigned char*) comp, &comp_len, workmem);
#else
comp_len = M_MAX_BUFSIZE;
if ( Z_OK != compress( (unsigned char*)&comp[0], (unsigned long*)&comp_len,(const unsigned char*)buff, uncomp_len) ) {
printf( "compress error!\n" );
assert(0);
}
#endif
assert( comp_len < Max_Comp_Len);
len = sizeof(struct lzo ) + comp_len ;
//construct return value
struct lzo *r =(struct lzo*) emalloc(len);
r->magic = 'lzod';
r->fileLen = len;
r->uncompLen = uncomp_len;
r->compLen = comp_len;
memcpy(r->comp, comp, comp_len);
string newname(name);
newname+=".lzo";
BinWrite((char*)newname.c_str(),(unsigned char *)r, len);
free (r);
}
/* if given a file name, it just compress it, with posix .lzo appended
* if given a directory name, it compress all file under it recursively, then package all file to 1 big file,
* each file concated one by one, with a small file header at the beginning.
*/
int main(int argc, char* argv[])
{
if( 2 != argc){
printf("usage: lzo filename or dir\n");
exit(-1);
}
DWORD att = GetFileAttributes(argv[1]);
if(att & FILE_ATTRIBUTE_DIRECTORY){
vector<string> names = WalkDir(argv[1]);
vector<string> filter(0);
for(vector<string>::iterator i=names.begin(); i!= names.end(); i++){
if(string::npos != (*i).find(".lzo"))
continue;
if(string::npos != (*i).find("vssver.scc"))
continue;
cout <<*i<<'\n';
lzoCompress((*i).c_str());
filter.push_back(string(*i));
}
char full[_MAX_PATH];
_fullpath(full,argv[1],_MAX_PATH);
string s(full);
string::size_type pos = s.find_last_of('\\');
s[pos+1] = 0;
package(filter,s.c_str(),(string(argv[1])+".pak").c_str());
return 0;
} else{
lzoCompress(argv[1]);
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -