unalz/UnAlzUtils.cpp

125 lines
3.4 KiB
C++
Raw Normal View History

2020-05-05 18:22:42 +00:00
#include <stdio.h>
#include <time.h>
#include "UnAlzUtils.h"
2020-05-05 18:22:53 +00:00
#ifdef _WIN32 // safe string ó<><C3B3>
# include "strsafe.h"
#endif
2020-05-05 18:22:42 +00:00
#ifdef _WIN32
# define I64FORM(x) "%"#x"I64d"
# define U64FORM(x) "%"#x"I64u"
#else
# define I64FORM(x) "%"#x"lld"
# define U64FORM(x) "%"#x"llu"
#endif
2020-05-05 18:22:53 +00:00
#define LEN_ATTR 6
2020-05-05 18:22:42 +00:00
time_t dosTime2TimeT(UINT32 dostime) // from INFO-ZIP src
{
struct tm t;
t.tm_isdst = -1;
t.tm_sec = (((int)dostime) << 1) & 0x3e;
t.tm_min = (((int)dostime) >> 5) & 0x3f;
t.tm_hour = (((int)dostime) >> 11) & 0x1f;
t.tm_mday = (int)(dostime >> 16) & 0x1f;
t.tm_mon = ((int)(dostime >> 21) & 0x0f) - 1;
t.tm_year = ((int)(dostime >> 25) & 0x7f) + 80;
return mktime(&t);
}
2020-05-05 18:22:53 +00:00
static void safe_strcat(char* dst, const char* src, size_t dst_size)
{
#ifdef _WIN32
StringCchCatExA(dst, dst_size, src, NULL, NULL, STRSAFE_FILL_BEHIND_NULL);
//lstrcatA(dst, src); // not safe!!
#else
strlcat(dst, src, dst_size);
#endif
}
2020-05-05 18:22:42 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////////
/// fileAttribute <20><> <20><>Ʈ<EFBFBD><C6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ٲ<EFBFBD><D9B2>ش<EFBFBD>.
/// @param buf - 5byte <20>̻<EFBFBD>
/// @param fileAttribute - ALZ_FILE_ATTRIBUTE <20><><EFBFBD><EFBFBD>
/// @return
/// @date 2005-06-23 <20><><EFBFBD><EFBFBD> 10:12:35
////////////////////////////////////////////////////////////////////////////////////////////////////
2020-05-05 18:22:53 +00:00
void FileAttr2Str(char szAttr[LEN_ATTR], BYTE fileAttribute)
2020-05-05 18:22:42 +00:00
{
2020-05-05 18:22:53 +00:00
szAttr[0] = 0;
2020-05-05 18:22:42 +00:00
if(fileAttribute&ALZ_FILEATTR_FILE)
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "A", LEN_ATTR);
2020-05-05 18:22:42 +00:00
else
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "_", LEN_ATTR);
2020-05-05 18:22:42 +00:00
if(fileAttribute&ALZ_FILEATTR_DIRECTORY)
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "D", LEN_ATTR);
2020-05-05 18:22:42 +00:00
else
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "_", LEN_ATTR);
2020-05-05 18:22:42 +00:00
if(fileAttribute&ALZ_FILEATTR_READONLY)
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "R", LEN_ATTR);
2020-05-05 18:22:42 +00:00
else
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "_", LEN_ATTR);
2020-05-05 18:22:42 +00:00
if(fileAttribute&ALZ_FILEATTR_HIDDEN)
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "H", LEN_ATTR);
2020-05-05 18:22:42 +00:00
else
2020-05-05 18:22:53 +00:00
safe_strcat(szAttr, "_", LEN_ATTR);
2020-05-05 18:22:42 +00:00
}
// alz <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ѵ<EFBFBD> ( -l <20>ɼ<EFBFBD> )
int ListAlz(CUnAlz* pUnAlz, const char* src)
{
CUnAlz::FileList::iterator i;
CUnAlz::FileList* list;
list = pUnAlz->GetFileList();
printf("\nListing archive: %s\n"
2020-05-05 18:22:44 +00:00
"\n Date & Time Attr Size Compressed Name\n",
2020-05-05 18:22:42 +00:00
src);
printf("------------------- ----- ------------ ------------ ------------\n");
2020-05-05 18:22:44 +00:00
// char szDate[64];
2020-05-05 18:22:42 +00:00
char szTime[64];
2020-05-05 18:22:53 +00:00
char szAttr[LEN_ATTR];
2020-05-05 18:22:42 +00:00
UINT64 totalUnCompressedSize = 0;
UINT64 totalCompressedSize = 0;
unsigned fileNum = 0;
time_t time;
struct tm* filetm;
for(i=list->begin(); i<list->end(); i++)
{
// time
time = dosTime2TimeT(i->head.fileTimeDate);
filetm = localtime(&time);
2020-05-05 18:22:44 +00:00
// strftime(szTime, 64, "%H:%M:%S", filetm);
// strftime(szDate, 64, "%Y-%m-%d", filetm);
strftime(szTime, 64, "%x %X", filetm); // use system locale
2020-05-05 18:22:42 +00:00
// attributes
FileAttr2Str(szAttr, i->head.fileAttribute);
2020-05-05 18:22:44 +00:00
printf("%19s %s " I64FORM(12) " " I64FORM(12) " %s%s\n",
szTime, szAttr, i->uncompressedSize,
2020-05-05 18:22:42 +00:00
i->compressedSize, i->fileName,
i->head.fileDescriptor & ALZ_FILE_DESCRIPTOR_ENCRYPTED ? "*" : "" );
++fileNum;
totalUnCompressedSize += i->uncompressedSize;
totalCompressedSize += i->compressedSize;
}
printf("------------------- ----- ------------ ------------ ------------\n");
printf(" " U64FORM(12) " " U64FORM(12) " %u file(s)\n",
totalUnCompressedSize, totalCompressedSize, fileNum);
return 0;
}