开发者

How to remove .zip file in c on windows? (error: Directory not empty) [closed]

开发者_运维技巧 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#include "win32-dirent.h"
#include <windows.h>
#include <io.h>
#include <direct.h>

#define MAXFILEPATH 1024

bool IsDirectory(char* path)
{
    WIN32_FIND_DATA w32fd;
    HANDLE hFindFile;

    hFindFile = FindFirstFile((PTCHAR)path, &w32fd);

    if(hFindFile == INVALID_HANDLE_VALUE)
    {
        return false;
    }
    return w32fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY); 
}

int RD(const char* folderName)
{
        DIR *dir;
        struct dirent *ent;

        dir = opendir(folderName);
        if(dir != NULL)
        {
                while((ent = readdir(dir)) != NULL)
                {
                        if(strcmp(ent->d_name , ".") == 0 ||
                                strcmp(ent->d_name, "..") == 0)
                        {
                                continue;
                        }

                        char fileName[MAXFILEPATH];
                        sprintf(fileName,"%s%c%s", folderName, '\\', ent->d_name);                        
                        if(IsDirectory(fileName))
                        {
                            RD(fileName);
                        }
                        else
                        {
                            unlink(fileName);
                        }
                }

                closedir(dir);

                //chmod(folderName, S_IWRITE | S_IREAD);
                if(_rmdir(folderName) != 0)perror(folderName);              

        }
        else
        {
                printf("%s <%s>\n","Could Not Open Directory.", folderName);
                return -1;
        }

        return 0;
}

int main(int argc, char* argv[])
{
        if(argc < 2)
        {
                printf("usage: ./a.out <target folder name>\n");
        return 1;
        }

        //RD(argv[1]);
        //_mkdir("12");
        //_mkdir("12\\34");
        //_rmdir("12\\34");
        //_rmdir("12");
        char buf[0xff];
        sprintf(buf, "unzip -x -q -d 1234 1234.zip");
        system(buf);

        RD("1234");

        //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56\\5.txt");
        //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56\\6.txt");
        //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\1_23.zip");
        //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\4.txt");
        //_rmdir("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56");
        //_rmdir("D:\\dev\\c\\project\\removeFolder\\Debug\\1234");

        return 0;
}

Output is:

--------------------------
Archive:  1234.zip
  inflating: 1234/4.txt
  inflating: 1234/56/5.txt
  inflating: 1234/56/6.txt
  inflating: 1234/1_23.zip
--------------------------


Your code looks feasible at first glance, you recursively clean up directories before attempting to remove them.

One thing comes immediately to mind, the possibility that you have the zip file opened up in another application, hence possibly locked.

You generally won't get "directory not empty" errors unless, well, the directory isn't empty.

For a start, change the line:

if(_rmdir(folderName) != 0)perror(folderName);

to:

if(_rmdir(folderName) != 0) {
    char buf[1000];
    sprintf(buf,"dir \"%s\"",foldername);
    system(buf);
    perror(folderName);
}

and that should hopefully tell you what directory you're in and what the offending files are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜