使用C#解压缩zip文件时路径中的非法字符

时间:2016-02-11 19:55:02

标签: c# file-io zip

我试图编写一个方法,将zip文件提取到目录,在提取的内容中查找文件,将该文件中的文本读取为字符串,然后返回该字符串。这是我的尝试

- (void)resizeImage:(UIImage*)image{

    NSData *finalData = nil;
    NSData *unscaledData = UIImagePNGRepresentation(image);

    if (unscaledData.length > 5000.0f ) {


       //if image size is greater than 5KB dividing its height and width maintaining proportions


        UIImage *scaledImage = [self imageWithImage:image andWidth:image.size.width/2 andHeight:image.size.height/2];
        finalData = UIImagePNGRepresentation(scaledImage);

        if (finalData.length > 5000.0f ) {

            [self resizeImage:scaledImage];
        }
        //scaled image will be your final image
    }
}

然而,尽管我的foreach循环替换了非法路径字符,但程序仍会在ZipFile.ExtractToDirectory行中的路径异常中抛出非法字符,无论我尝试使用哪个目录,我都不知道为什么。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

根据similar post,您可能会遇到目标zip文件中的文件名问题;它对您指定的zip文件名或目录没有问题。尝试手动提取文件内容以查看是否存在异常文件名。

答案 1 :(得分:1)

您可以遍历所有条目并清理文件名,然后使用我在此处写的代码段提取它们:ZipFile.ExtractToDirectory "Illegal characters in path"