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

📄 pathname.c

📁 给出了 zip 压缩算法的完整实现过程。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  Copyright (c) 1990-2005 Info-ZIP.  All rights reserved.  See the accompanying file LICENSE, version 2004-May-22 or later  (the contents of which are also included in zip.h) for terms of use.  If, for some reason, both of these files are missing, the Info-ZIP license  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.htmlhtml*//*---------------------------------------------------------------------------  pathname.c  Function dealing with the pathname. Mostly C-string work.  ---------------------------------------------------------------------------*//*****************************************************************************//*  Includes                                                                 *//*****************************************************************************/#include <string.h>#include <stdio.h>#include <unistd.h>#include <sound.h>#include "pathname.h"#include "helpers.h"#include "macstuff.h"/*****************************************************************************//*  Global Vars                                                              *//*****************************************************************************/const char  ResourceMark[] = "XtraStuf.mac:";  /* see also macos.c */#include "zip.h"/*****************************************************************************//*  Functions                                                                *//*****************************************************************************//***  return volumename from pathname***/unsigned short GetVolumeFromPath(const char *FullPath, char *VolumeName){const char *VolEnd, *tmpPtr1;char *tmpPtr2 = VolumeName;AssertStr(FullPath,"GetVolumeFromPath")for (VolEnd = FullPath; *VolEnd != '\0' && *VolEnd != ':'; VolEnd++)      ;if (*VolEnd == '\0') return 0;for (tmpPtr1 = FullPath; tmpPtr1 != VolEnd;)    {    *tmpPtr2++ = *tmpPtr1++;    }*tmpPtr2 = '\0';return (unsigned short) strlen(VolumeName);}/***********************************//* Function FindNewExtractFolder() *//***********************************/char *FindNewExtractFolder(char *ExtractPath){char buffer[NAME_MAX], *tmpPtr, *namePtr;short count = 0, folderCount = 0;OSErr err;FSSpec Spec;long theDirID;Boolean isDirectory;unsigned short namelen, pathlen = strlen(ExtractPath);AssertStr(ExtractPath,"FindNewExtractFolder ExtractPath == NULL")for (tmpPtr = ExtractPath; *tmpPtr; tmpPtr++)    if (*tmpPtr == ':')        {        folderCount++;        namePtr = tmpPtr;        }if (folderCount > 1)    namelen = strlen(namePtr);else    namelen = strlen(ExtractPath);for (count = 0; count < 99; count++)    {    memset(buffer,0,sizeof(buffer));    if (namelen >= 28)        ExtractPath[pathlen-2] = 0x0;    else        ExtractPath[pathlen-1] = 0x0;    sprintf(buffer,"%s%d",ExtractPath,count);    GetCompletePath(ExtractPath, buffer, &Spec,&err);    err = FSpGetDirectoryID(&Spec, &theDirID, &isDirectory);    if (err == -43) break;    }/* Foldernames must always end with a colon  */sstrcat(ExtractPath,":");return ExtractPath;}/***  creates an archive file name***/void createArchiveName(char *thePath){char *tmpPtr, *namePtr;short folderCount = 0;unsigned short namelen, pathlen = strlen(thePath);if (thePath[pathlen-1] == ':') thePath[pathlen-1] = 0x0;for (tmpPtr = thePath; *tmpPtr; tmpPtr++)    if (*tmpPtr == ':')        {        folderCount++;        namePtr = tmpPtr;        }namelen = strlen(namePtr);    /* we have to eliminate illegal chars:     * The name space for Mac filenames and Zip filenames (unix style names)     * do both include all printable extended-ASCII characters.  The only     * difference we have to take care of is the single special character     * used as path delimiter:     * ':' on MacOS and '/' on Unix and '\' on Dos.     * So, to convert between Mac filenames and Unix filenames without any     * loss of information, we simply interchange ':' and '/'.  Additionally,     * we try to convert the coding of the extended-ASCII characters into     * InfoZip's standard ISO 8859-1 codepage table.     */  MakeCompatibleString(namePtr, '/', '_', '.', '-', -1); /* Avoid filenames like: "Archive..zip"  */if (thePath[pathlen-1] == '.')    {    thePath[pathlen-1] = 0;    }if (folderCount >= 1)    { /* path contains at least one folder */    if (namelen >= 28)        {        pathlen = pathlen-4;        }    thePath[pathlen]   = '.';    thePath[pathlen+1] = 'z';    thePath[pathlen+2] = 'i';    thePath[pathlen+3] = 'p';    thePath[pathlen+4] = 0x0;    return;    }else    {  /* path contains no folder */    FindDesktopFolder(thePath);    createArchiveName(thePath);    }}/*** finds the desktop-folder on a volume with** largest amount of free-space.*/void FindDesktopFolder(char *Path){char buffer[255];FSSpec  volumes[50];        /* 50 Volumes should be enough */short   actVolCount, volIndex = 1, VolCount = 0;OSErr   err;short     i, foundVRefNum;FSSpec spec;UnsignedWide freeBytes;UnsignedWide totalBytes;UnsignedWide MaxFreeBytes;err = OnLine(volumes, 50, &actVolCount, &volIndex);printerr("OnLine:", (err != -35) && (err != 0), err, __LINE__, __FILE__, "");MaxFreeBytes.hi = 0;MaxFreeBytes.lo = 0;for (i=0; i < actVolCount; i++)    {    XGetVInfo(volumes[i].vRefNum,              volumes[i].name,              &volumes[i].vRefNum,              &freeBytes,              &totalBytes);    if (MaxFreeBytes.hi < freeBytes.hi) {        MaxFreeBytes.hi = freeBytes.hi;        MaxFreeBytes.lo = freeBytes.lo;        foundVRefNum = volumes[i].vRefNum;    }    if ((freeBytes.hi == 0) && (MaxFreeBytes.lo < freeBytes.lo)) {        MaxFreeBytes.hi = freeBytes.hi;        MaxFreeBytes.lo = freeBytes.lo;        foundVRefNum = volumes[i].vRefNum;    }} FSpFindFolder(foundVRefNum, kDesktopFolderType,            kDontCreateFolder,&spec); GetFullPathFromSpec(buffer, &spec , &err); sstrcat(buffer,Path); sstrcpy(Path,buffer);}/***  return the path without the filename***/char *TruncFilename(char *DirPath, const char *FilePath){char *tmpPtr;char *dirPtr = NULL;AssertStr(DirPath,"TruncFilename")Assert_it(Spec,"TruncFilename","")sstrcpy(DirPath, FilePath);for (tmpPtr = DirPath; *tmpPtr; tmpPtr++)    if (*tmpPtr == ':')        dirPtr = tmpPtr;if (dirPtr)    *++dirPtr = '\0';else    printerr("TruncFilename: FilePath has no Folders", -1,         -1, __LINE__, __FILE__, FilePath);return DirPath;}/***  return only filename***/char *GetFilename(char *FileName, const char *FilePath){const char *tmpPtr;const char *dirPtr = NULL;Assert_it(FileName,"GetFilename","")Assert_it(FilePath,"GetFilename","")for (tmpPtr = FilePath; *tmpPtr; tmpPtr++)    {    if (*tmpPtr == ':')        {        dirPtr = tmpPtr;        }    }if (dirPtr)    {    ++dirPtr;  /* jump over the ':' */    }else    {    return strcpy(FileName, FilePath); /* FilePath has no Folders */    }return strcpy(FileName, dirPtr);}/***  return fullpathname from folder/dir-id***/char *GetFullPathFromID(char *CompletePath, short vRefNum, long dirID,                        ConstStr255Param name, OSErr *err){FSSpec      spec;    *err = FSMakeFSSpecCompat(vRefNum, dirID, name, &spec);    printerr("FSMakeFSSpecCompat:", (*err != -43) && (*err != 0), *err,             __LINE__, __FILE__, "");    if ( (*err == noErr) || (*err == fnfErr) )        {        return GetFullPathFromSpec(CompletePath, &spec, err);        }return NULL;}

⌨️ 快捷键说明

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