?? archiver_old.cpp
字號:
// Created:11-16-98
// By Jeff Connelly
// Archiver example using ComprLib
#include "comprlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#if 0
************************** Archive structure ******************************
This header is present only at the beginning:
OFFSET Count TYPE Description
0x0000 4 char ID="ARCH"
0x0004 4 char ID="IVE" followed by 1Ah
0x0008 1 dword Files in archive (up to over a million!)
For each file, there is:
OFFSET Count TYPE Description
0x0000 1 byte Misc flags, bitmapped:
Bits numbered: 7654 3210
0 - Compressed
1 - CRC-32 stored
2 - Comment
3 - File in archive (else deleted)
4 to 7 - Compression method if compressed
(see table 0x0000)
0x0000 1 dword Size of file in the archive (not orig size)
???? 1 dword If compressed: original size
???? 1 dword If CRC-32 stored: CRC-32
???? 1 word If comment: comment length
???? ? char If comment: comment text
???? ? byte File contents
TABLE 0x0000: Compression methods
0000 - Not used, set if not compressed
0001 - RLE method 1
0010 - RLE method 2
0011 - LZSS
0100 - Inverted (encryption sort of)
#endif
// Help message
const char* help = "Archives files to an archiver\n"
"\tARCHIVER command archive [files...]\n"
"Valid commands are: \n"
"a Add files to archive\n"
"x Extract files from archive\n"
"l List files in archive\n"
"\n";
// Archive
FILE* arc;
char* archive;
unsigned long files_in_archive;
// struct representing part of a file in the archive. If would be nice if
// we could just fwrite this to the file, but it contains things that are
// only included if some flags are set.
typedef struct
{
char flags;
unsigned long size;
unsigned long origsize;
unsigned long crc32;
unsigned short commentlen;
char* comment;
char* file;
} MEMBER;
#define FLAG_COMPRESSED 0x01
#define FLAG_CRC32 0x02
#define FLAG_COMMENT 0x04
#define FLAG_INARCHIVE 0x08
// Opens 'archive' and validates
bool open_archive()
{
if (!archive)
return false;
arc = fopen(archive, "r+b");
fflush(arc);
if (!arc)
return false;
char* sig = (char*)malloc(9);
fread (sig, 1, 8, arc);
sig[8] = 0;
if (!strcmp(sig, "ARCHIVE\032"))
{
fclose(arc);
return false;
}
fread (&files_in_archive, 1, 4, arc);
free (sig);
}
unsigned long file_size(FILE* fp)
{
unsigned long pos = ftell(fp);
fseek(fp, 0, SEEK_END);
unsigned long ret = ftell(fp);
fseek(fp, pos, SEEK_SET);
return ret;
}
// Writes the file 'member' to the archive
void read_member(MEMBER& file)
{
fread (&file.flags, 1, 1, arc);
fread (&file.size, 1, 4, arc);
if (file.flags & FLAG_COMPRESSED)
fread (&file.origsize, 1, 4, arc);
else
file.origsize = 0;
if (file.flags & FLAG_CRC32)
fread (&file.crc32, 1, 4, arc);
else
file.crc32 = 0;
if (file.flags & FLAG_COMMENT)
{
fread (&file.commentlen, 1, 4, arc);
file.comment = (char*)xmalloc(file.commentlen);
fread (file.comment, 1, file.commentlen, arc);
} else {
file.commentlen = 0;
file.comment = NULL;
}
file.file = (char*)xmalloc(file.size);
fread (file.file, 1, file.size, arc);
}
// Frees the memory allocated
void destroy_member(MEMBER& file)
{
xfree(file.file);
xfree(file.comment);
}
// Writes 'file' to the archive
void write_member(MEMBER& file)
{
fwrite (&file.flags, 1, 1, arc);
fwrite (&file.size, 1, 4, arc);
if (file.flags & FLAG_COMPRESSED)
fwrite (&file.origsize, 1, 4, arc);
if (file.flags & FLAG_CRC32)
fwrite (&file.crc32, 1, 4, arc);
if (file.flags & FLAG_COMMENT)
{
fwrite (&file.commentlen, 1, 4, arc);
fwrite (file.comment, 1, file.commentlen, arc);
}
fwrite (file.file, 1, file.size, arc);
}
// Add 'count' 'files' to the archive
void add_files(int count, char* files[])
{
int i;
MEMBER file;
arc = fopen(archive, "ab");
if (!arc)
{
printf("Cannot open %s\n", archive);
exit (1);
}
for (i = 0; i < count; ++i)
{
printf ("Writing %s...\n", files[i]);
FILE* fp = fopen(files[i], "rb");
if (!fp)
{
printf ("Cannot open %s\n", files[i]);
exit (3);
}
file.size = file_size(fp);
file.file = (char*)xmalloc(file.size);
fread(file.file, 1, file.size, arc);
file.flags = 0;
write_member(file);
xfree(file.file);
fclose(fp);
}
}
void extract_files(int count, char* files[])
{
}
void list_files(int count, char* files[])
{
}
int main(int argc, char* argv[])
{
char cmd;
if (argc == 1)
{
printf(help);
exit (0);
}
cmd = tolower(argv[1][0]);
if (argc > 2) // If the archive filename is specified, copy it
{ // to 'archive'
archive = (char*)xmalloc(strlen(argv[2]));
strcpy (archive, argv[2]);
} else { // No archive filename specified
archive = NULL;
}
switch (cmd)
{
case 'a': add_files(argc - 2, &argv[3]); break;
case 'x': extract_files(argc - 2, &argv[3]); break;
case 'l': list_files(argc - 2, &argv[3]); break;
default: break;
};
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -