TransformDirectory() public method

Transform a Zip directory name to a windows directory name.
public TransformDirectory ( string name ) : string
name string The directory name to transform.
return string
示例#1
0
        public static bool unzipFile(Stream inputStream, string targetDirectory, bool overwrite, OnUnzipProgress callback)
        {
            bool ret = false;

            callback(0, 0);
            using (ZipFile zipFile_ = new ZipFile(inputStream))
            {
                int       totalBytes = (int)zipFile_.unzipSize;
                UnzipCach cach       = new UnzipCach();
                cach.start(totalBytes, callback, overwrite);

                INameTransform extractNameTransform_      = new WindowsNameTransform(targetDirectory);
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    try
                    {
                        ZipEntry entry = (ZipEntry)enumerator.Current;
                        if (entry.IsFile)
                        {
                            string fileName = extractNameTransform_.TransformFile(entry.Name);
                            string dirName  = Path.GetDirectoryName(Path.GetFullPath(fileName));
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                            Stream source = zipFile_.GetInputStream(entry);
                            cach.addFile(fileName, (int)entry.Size, source);
                            source.Close();
                        }
                        else
                        {
                            string dirName = extractNameTransform_.TransformDirectory(entry.Name);
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        cach.setState(UnzipCach.State.Error);
                        //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES);
                    }

                    if (cach.isError())
                    {
                        break;
                    }
                }

                cach.setState(UnzipCach.State.Ok);
                ret = cach.stop();
                callback(1, ret?0:1);
                return(ret);
            }
        }
示例#2
0
 public void NameTooLong()
 {
     WindowsNameTransform wnt = new WindowsNameTransform();
     string veryLong = new string('x', 261);
     try {
         wnt.TransformDirectory(veryLong);
         Assert.Fail("Expected an exception");
     }
     catch (PathTooLongException) {
     }
 }
示例#3
0
 public void LengthBoundaryOk()
 {
     WindowsNameTransform wnt = new WindowsNameTransform();
     string veryLong = "c:\\" + new string('x', 260);
     try {
         string transformed = wnt.TransformDirectory(veryLong);
     }
     catch {
         Assert.Fail("Expected no exception");
     }
 }
示例#4
0
        static void unzipFileThread()
        {
            //LogManager.GetInstance().LogMessage("unzip begin outpath="+_targetDirectory, LogManager.ModuleFilter.RES);
            _callback(0, 0);
            bool ret = false;

            using (ZipFile zipFile_ = new ZipFile(_inputStream))
            {
                int       totalBytes = (int)zipFile_.unzipSize;
                UnzipCach cach       = new UnzipCach();
                cach.start(totalBytes, _callback);

                INameTransform extractNameTransform_      = new WindowsNameTransform(_tmpDirectory);
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    try
                    {
                        ZipEntry entry = (ZipEntry)enumerator.Current;
                        if (entry.IsFile)
                        {
                            string fileName = extractNameTransform_.TransformFile(entry.Name);
                            string dirName  = Path.GetDirectoryName(Path.GetFullPath(fileName));
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                            Stream source = zipFile_.GetInputStream(entry);
                            cach.addFile(fileName, (int)entry.Size, source);
                            source.Close();
                        }
                        else
                        {
                            string dirName = extractNameTransform_.TransformDirectory(entry.Name);
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        cach.setState(UnzipCach.State.Error);
                        //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES);
                    }

                    if (cach.isError())
                    {
                        break;
                    }
                }

                cach.setState(UnzipCach.State.Ok);
                if (cach.stop())
                {
                    try
                    {
                        Directory.Move(_tmpDirectory, _targetDirectory);
                        ret = true;
                    }
                    catch (Exception e)
                    {
                        //LogManager.GetInstance().LogException("unzip rename dir error.", e);
                    }
                }
                _callback(1, ret?0:1);
                //LogManager.GetInstance().LogMessage("unzip end", LogManager.ModuleFilter.RES);
                _inputStream = null;
                _callback    = null;
            }
        }
示例#5
0
        object extractZip(   object zipFileName, string rootDirectory,IStringFilter nf, IStringFilter df)
        {
            object ret = null;
            WindowsNameTransform extractNameTransform = new WindowsNameTransform(rootDirectory);
            Dictionary<string, bool> dirs = new Dictionary<string, bool>(StringComparer.InvariantCultureIgnoreCase);

            Stream str;
            if (zipFileName is byte[])
                str = new MemoryStream((byte[])zipFileName);
            else
                str=new SeekableStream(Context.OpenStream(zipFileName.ToString()),true);
            using (ZipFile zip = new ZipFile(str))
            {
                if (Password != null)
                    zip.Password = Context.TransformStr(Password, Transform);

                foreach (ZipEntry entry in zip)
                {

                    string targetName = null;
                    if (entry.IsFile)
                    {
                        targetName = extractNameTransform.TransformFile(entry.Name);
                        if (!UsePath)
                            targetName = Path.Combine(rootDirectory, Path.GetFileName(targetName));
                    }
                    else if (entry.IsDirectory)
                    {
                        if (UsePath)
                            targetName = extractNameTransform.TransformDirectory(entry.Name);
                        else
                            targetName = rootDirectory;
                    }
                    if (string.IsNullOrEmpty(targetName))
                        continue;
                    if (!Hidden)
                    {
                        if (isDos(entry) && (((FileAttributes) entry.ExternalFileAttributes) & (FileAttributes.Hidden)) != 0)
                            continue;
                    }
                    if (string.IsNullOrEmpty(entry.Name))
                        continue;
                    var n = new ZipFSEntry(entry, ZipTime);
                    if ((entry.IsFile && df.IsMatch(Path.GetDirectoryName(n.FullName)) && nf.IsMatch(n.Name)) ||
                        (entry.IsDirectory && df.IsMatch(n.FullName)))
                    {
                        object r = extract(zip, rootDirectory, targetName, entry, dirs);
                        if (r!=null)
                            return r;
                    }
                }
            }
            return ret;
        }