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

📄 ccmprs.cpp

📁 A command-line file compression utility for Windows NT. It allows you to select files for NTFS fil
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    } while (FALSE);

    return bRet;
}


//**************************************************************************
//  Main routine to do conditional file compression/decompression.
//**************************************************************************
int DoIt(LPSTR szFile)
{
    LPWIN32_FIND_DATA pFind  = NULL;
    HANDLE            hFind  = INVALID_HANDLE_VALUE;
    HANDLE            hFile  = INVALID_HANDLE_VALUE;
    int               nRet   = 0;
    int               cSlash = '\\';
    LPSTR             pszPath, p, pszFileName;
    DWORD             dwErr;
    static char       szExt[_MAX_PATH];

    do
    {
        // Allocate file-find structure.
        pFind = (LPWIN32_FIND_DATA)malloc(sizeof(WIN32_FIND_DATA));
        if (!pFind)
        {
            printf("Out of memory!\n");
            nRet = 1;
            break;
        }

        // Find start of file name within path, and get the slash character
        // being used.
        p = szFile + strlen(szFile);
        while (p != szFile && *p != '\\' && *p != '/' && *p != ':')
            p--;
        if (*p == '\\' || *p == '/' || *p == ':')
        {
            if (*p++ == '/')
                cSlash = '/';
        }
        pszFileName = p;

        // If we are recursing, scan for directories and recurse into any that we find.
        if (bRecurse)
        {
            char *szSaveFileName = (char *)malloc(strlen(pszFileName) + 1);
            if (szSaveFileName)
            {
                // Save file name portion of file specification, and temporarily
                // replace it with "*.*".
                strcpy(szSaveFileName, pszFileName);
                strcpy(pszFileName, "*.*");

                // For each directory...
                hFind = FindFirstFile(szFile, pFind);
                if (hFind != INVALID_HANDLE_VALUE)
                {
                    do
                    {
                        if ((pFind->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && pFind->cFileName[0] != '.')
                        {
                            // Create new filespec with directory name appended to directory portion.
                            pszPath = (LPSTR)malloc(strlen(szFile) + strlen(pFind->cFileName) + 6);
                            if (!pszPath)
                            {
                                printf("Out of memory!\n");
                                nRet = 1;
                                break;
                            }
                            strcpy(pszPath, szFile);
                            p = pszPath + (pszFileName - szFile);
                            strcpy(p, pFind->cFileName);
                            p += strlen(p);
                            *p++ = cSlash;
                            strcpy(p, szSaveFileName);

                            // Do the recursion.
                            nRet = DoIt(pszPath);

                            // Clean up.
                            free(pszPath);

                            // Bail now on error.
                            if (nRet)
                                break;
                        }

                    } while (FindNextFile(hFind, pFind));

                    // Clean up.
                    FindClose(hFind);
                }

                // Restore original file name.
                strcpy(pszFileName, szSaveFileName);
                free(szSaveFileName);
            }
        }

        // Start search for files.
        hFind = FindFirstFile(szFile, pFind);
        if (hFind == INVALID_HANDLE_VALUE)
        {
            //printf("The specified file path, %s, is invalid.\n", szFile);
            //nRet = 1;
            break;
        }

        // For each file...
        do
        {
            // Skip directories.
            if (pFind->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                continue;

            // Ignore the file if its extension is in the "ignore extensions" list.
            if (szIgnoreExts)
            {
                for (p = pFind->cFileName + strlen(pFind->cFileName); p != pFind->cFileName && *p != '.'; p--) ;
                if (*p == '.')
                {
                    p++;
                    if (*p)
                    {
                        strcpy(szExt, p);
                        strlwr(szExt);
                        strcpy(szExts, szIgnoreExts);
                        p = strtok(szExts, " ,");
                        while (p)
                        {
                            if (!strcmp(p, szExt))
                                break;
                            p = strtok(NULL, " ,");
                        }
                        if (p)
                            continue;
                    }
                }
            }

            // Make full path and file name.
            strcpy(szTemp, szFile);
            strcpy(szTemp + (pszFileName - szFile), pFind->cFileName);

            // Get the file's compressed and uncompressed file sizes.
            DWORD dwFileSizeHi, dwCompSizeHi;
            hFile = CreateFile(szTemp, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_READONLY, NULL);
            if (hFile == INVALID_HANDLE_VALUE)
                continue;
            DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
            CloseHandle(hFile);
            if (dwFileSizeLo == 0xFFFFFFFF && (dwErr = GetLastError()) != NO_ERROR)
                continue;
            DWORD dwCompSizeLo = GetCompressedFileSize(szTemp, &dwCompSizeHi);
            if (dwCompSizeLo == 0xFFFFFFFF && (dwErr = GetLastError()) != NO_ERROR)
                continue;
            double filesize = (double)dwFileSizeHi * 4294967296.0 + (double)dwFileSizeLo;
            double compsize = (double)dwCompSizeHi * 4294967296.0 + (double)dwCompSizeLo;

            // Compress or decompress the file depending on compression state and flags.
            if (compsize == filesize)
            {
                // File is not compressed.  Skip it if it does not meet minimum size
                // requirement for compression.
                if (filesize < MinFileSize)
                    continue;

                // Compress the file.
                if (!SetFileCompression(szTemp, TRUE))
                    continue;

                // Get the new compressed size.
                dwCompSizeLo = GetCompressedFileSize(szTemp, &dwCompSizeHi);
                if (dwCompSizeLo == 0xFFFFFFFF && GetLastError() != NO_ERROR)
                {
                    SetFileCompression(szTemp, FALSE);
                    continue;
                }
                compsize = (double)dwCompSizeHi * 4294967296.0 + (double)dwCompSizeLo;

                // If the file did not compress enough, decompress it.
                if (compsize > filesize * threshold)
                    SetFileCompression(szTemp, FALSE);
                else
                {
                    sizechange += compsize - filesize;
                    nCompressed++;
                    if (!bQuiet)
                        printf("%s compressed.\n", szTemp);
                }
            }
            else
            {
                // File is compressed.  If the -d flag was not specified, just skip it.
                if (!bDecompress)
                    continue;

                // If the compression is not enough, or the file's original size is not
                // big enough, decompress the file.
                if (compsize > filesize * threshold)
                {
                    SetFileCompression(szTemp, FALSE);
                    if (!bQuiet)
                        printf("%s decompressed.\n", szTemp);
                    nDecompressed++;
                    sizechange += filesize - compsize;
                }
                else if (filesize < MinFileSize)
                    SetFileCompression(szTemp, FALSE);
            }

        } while (FindNextFile(hFind, pFind));

    } while (FALSE);

    // Clean up.
    if (hFind != INVALID_HANDLE_VALUE)
        FindClose(hFind);
    if (pFind)
        free(pFind);

    return nRet;
}


//**************************************************************************
//  The main entry point, the head honcho, the Big Cheeze...
//**************************************************************************
int main(int ac, char *av[])
{
    int nRet;

    // If no parameters are specified, display usage instructions.
    if (ac < 2)
    {
        DisplayUsage();
        return 1;
    }

    // Read command line parameters.
    if (!GetParameters(ac, av))
        return 1;

    // Do the compression/decompression.
    nRet = DoIt(szFileSpec);

    // Report results.
    printf("\n%d file(s) compressed, %d file(s) decompressed, ", nCompressed, nDecompressed);
    if (sizechange <= 0)
        printf("%.0lf bytes more free space.\n", -sizechange);
    else
        printf("%.0lf bytes less free space.\n", sizechange);

    // Clean up.
    if (szExts)
        free(szExts);
    if (szIgnoreExts)
        free(szIgnoreExts);

    return nRet;
}

⌨️ 快捷键说明

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