public zipimporter(CodeContext /*!*/ context, object pathObj, [ParamDictionary] IDictionary <object, object> kwArgs) { PlatformAdaptationLayer pal = context.LanguageContext.DomainManager.Platform; string prefix, input, path; if (pathObj == null) { throw PythonOps.TypeError("must be string, not None"); } if (!(pathObj is string)) { throw PythonOps.TypeError("must be string, not {0}", pathObj.GetType()); } if (kwArgs.Count > 0) { throw PythonOps.TypeError("zipimporter() does not take keyword arguments"); } path = pathObj as string; if (path.Length == 0) { throw MakeError(context, "archive path is empty"); } if (path.Length > MAXPATHLEN) { throw MakeError(context, "archive path too long"); } string buf = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); input = buf; path = string.Empty; prefix = string.Empty; try { while (!string.IsNullOrEmpty(buf)) { if (pal.FileExists(buf)) { path = buf; break; } buf = Path.GetDirectoryName(buf); } } catch { throw MakeError(context, "not a Zip file"); } if (!string.IsNullOrEmpty(path)) { PythonDictionary zip_directory_cache = context.LanguageContext.GetModuleState( _zip_directory_cache_key) as PythonDictionary; if (zip_directory_cache != null && zip_directory_cache.ContainsKey(path)) { _files = zip_directory_cache[path] as PythonDictionary; } else { _files = ReadDirectory(context, path); zip_directory_cache.Add(path, _files); } } else { throw MakeError(context, "not a Zip file"); } _prefix = input.Replace(path, string.Empty); // add trailing SEP if (!string.IsNullOrEmpty(_prefix) && !_prefix.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) { _prefix = _prefix.Substring(1); _prefix += Path.DirectorySeparatorChar; } _archive = path; }
/// <summary> /// Sets a variable in the local scope. /// </summary> internal void SetVariable(string name, object value) { Dict.Add(name, value); }
/// <summary> /// Given a path to a Zip archive, build a dict, mapping file names /// (local to the archive, using SEP as a separator) to toc entries. /// /// A toc_entry is a tuple: /// (__file__, # value to use for __file__, available for all files /// compress, # compression kind; 0 for uncompressed /// data_size, # size of compressed data on disk /// file_size, # size of decompressed data /// file_offset, # offset of file header from start of archive /// time, # mod time of file (in dos format) /// date, # mod data of file (in dos format) /// crc, # crc checksum of the data /// ) /// Directories can be recognized by the trailing SEP in the name, /// data_size and file_offset are 0. /// </summary> /// <param name="archive"></param> /// <returns></returns> private PythonDictionary ReadDirectory(CodeContext context, string archive) { string path, name = string.Empty; BinaryReader fp = null; int header_position, header_size, header_offset, count, compress; int time, date, crc, data_size, name_size, file_size, file_offset; int arc_offset; // offset from beginning of file to start of zip-archive PythonDictionary files = null; byte[] endof_central_dir = new byte[22]; if (archive.Length > MAXPATHLEN) { throw PythonOps.OverflowError("Zip path name is too long"); } path = archive; try { try { fp = new BinaryReader(new FileStream(archive, FileMode.Open, FileAccess.Read)); } catch { throw MakeError(context, "can't open Zip file: '{0}'", archive); } if (fp.BaseStream.Length < 22) { throw MakeError(context, "can't read Zip file: '{0}'", archive); } fp.BaseStream.Seek(-22, SeekOrigin.End); header_position = (int)fp.BaseStream.Position; if (fp.Read(endof_central_dir, 0, 22) != 22) { throw MakeError(context, "can't read Zip file: '{0}'", archive); } if (BitConverter.ToUInt32(endof_central_dir, 0) != 0x06054B50) { // Bad: End of Central Dir signature fp.Close(); throw MakeError(context, "not a Zip file: '{0}'", archive); } header_size = BitConverter.ToInt32(endof_central_dir, 12); header_offset = BitConverter.ToInt32(endof_central_dir, 16); arc_offset = header_position - header_offset - header_size; header_offset += arc_offset; files = new PythonDictionary(); path += Path.DirectorySeparatorChar; // Start of Central Directory count = 0; while (true) { name = string.Empty; fp.BaseStream.Seek(header_offset, SeekOrigin.Begin); // Start of file header int l = fp.ReadInt32(); if (l != 0x02014B50) { break; // Bad: Central Dir File Header } fp.BaseStream.Seek(header_offset + 10, SeekOrigin.Begin); compress = fp.ReadInt16(); time = fp.ReadInt16(); date = fp.ReadInt16(); crc = fp.ReadInt32(); data_size = fp.ReadInt32(); file_size = fp.ReadInt32(); name_size = fp.ReadInt16(); header_size = 46 + name_size + fp.ReadInt16() + fp.ReadInt16(); fp.BaseStream.Seek(header_offset + 42, SeekOrigin.Begin); file_offset = fp.ReadInt32() + arc_offset; if (name_size > MAXPATHLEN) { name_size = MAXPATHLEN; } for (int i = 0; i < name_size; i++) { char c = fp.ReadChar(); if (c == '/') { c = Path.DirectorySeparatorChar; } name += c; } header_offset += header_size; PythonTuple t = PythonTuple.MakeTuple(path + name, compress, data_size, file_size, file_offset, time, date, crc); files.Add(name, t); count++; } } finally { fp?.Close(); } return(files); }
/// <summary> /// 用于测试 /// </summary> /// <returns></returns> public static ArrayList test() { ArrayList returnList = new ArrayList(); string serverpath = "D:\\projects\\recommandSystem\\UserCFRecommend.py"; ScriptRuntime pyRuntime = Python.CreateRuntime(); ScriptEngine Engine = pyRuntime.GetEngine("python"); ICollection <string> Paths = Engine.GetSearchPaths(); Paths.Add("D:\\Anaconda2-32\\Lib"); Paths.Add("D:\\Anaconda2-32\\DLLs"); Paths.Add("D:\\Anaconda2-32\\Lib\\site-packages"); ScriptScope pyScope = Engine.CreateScope(); //Engine.ImportModule("pandas"); //Engine.ImportModule("math"); //Engine.ImportModule("pickle"); Engine.SetSearchPaths(Paths); IronPython.Runtime.PythonDictionary pyDic = new IronPython.Runtime.PythonDictionary(); ArrayList A = new ArrayList(); A.Add('a'); A.Add('b'); A.Add('d'); ArrayList B = new ArrayList(); B.Add('a'); B.Add('c'); ArrayList C = new ArrayList(); C.Add('b'); C.Add('e'); ArrayList D = new ArrayList(); D.Add('c'); D.Add('d'); D.Add('e'); pyDic.Add('A', A); pyDic.Add('B', B); pyDic.Add('C', C); pyDic.Add('D', D); dynamic obj = Engine.ExecuteFile(serverpath, pyScope); IronPython.Runtime.List result = obj.recommmend_user_cf('A', pyDic, 3); for (int i = 0; i < result.Count; i++) { IronPython.Runtime.PythonTuple pySet = (IronPython.Runtime.PythonTuple)result.ElementAt(i); Console.WriteLine(pySet.ElementAt(0)); Console.WriteLine(pySet.ElementAt(1)); returnList.Add(pySet.ElementAt(0)); } Console.WriteLine(result); return(returnList); }