public static File createTemp(string prefix, string suffix, File dir) { if (prefix == null || prefix.Length == 0) prefix = "fan"; if (suffix == null) suffix = ".tmp"; string parent = null; if (dir == null) { parent = System.IO.Path.GetTempPath(); } else { if (!(dir is LocalFile)) throw IOErr.make("Dir is not on local file system: " + dir).val; parent = ((LocalFile)dir).m_file.FullName; } try { string name = parent + '\\' + prefix + suffix; int count = 1; while (System.IO.File.Exists(name)) name = parent + '\\' + prefix + (count++) + suffix; LocalFile temp = new LocalFile(new System.IO.FileInfo(name)); temp.create(); return temp; } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
public ZipReturn ZipFileOpenWriteStream(bool raw, bool trrntzip, string filename, ulong uncompressedSize, ushort compressionMethod, out Stream stream) { stream = null; if (ZipOpen != ZipOpenType.OpenWrite) return ZipReturn.ZipWritingToInputFile; LocalFile lf = new LocalFile(_zipFs, filename); ZipReturn retVal = lf.LocalFileOpenWriteStream(raw, trrntzip, uncompressedSize, compressionMethod, out stream); _localFiles.Add(lf); return retVal; }
public ZipReturn ZipFileOpenReadStreamQuick(ulong pos, bool raw, out Stream stream, out ulong streamSize, out ushort compressionMethod) { LocalFile tmpFile = new LocalFile(_zipFs) { LocalFilePos = pos }; _localFiles.Clear(); _localFiles.Add(tmpFile); ZipReturn zr = tmpFile.LocalFileHeaderReadQuick(); if (zr != ZipReturn.ZipGood) { stream = null; streamSize = 0; compressionMethod = 0; return zr; } _readIndex = 0; return tmpFile.LocalFileOpenReadStream(raw, out stream, out streamSize, out compressionMethod); }
public ZipReturn ZipFileOpen(string newFilename, long timestamp, bool readHeaders) { ZipFileClose(); _pZipStatus = ZipStatus.None; _zip64 = false; _centerDirStart = 0; _centerDirSize = 0; _zipFileInfo = null; try { if (!IO.File.Exists(newFilename)) { ZipFileClose(); return ZipReturn.ZipErrorFileNotFound; } _zipFileInfo = new IO.FileInfo(newFilename); if (_zipFileInfo.LastWriteTime != timestamp) { ZipFileClose(); return ZipReturn.ZipErrorTimeStamp; } int errorCode = IO.FileStream.OpenFileRead(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); if (errorCode == 32) return ZipReturn.ZipFileLocked; return ZipReturn.ZipErrorOpeningFile; } } catch (PathTooLongException) { ZipFileClose(); return ZipReturn.ZipFileNameToLong; } catch (IOException) { ZipFileClose(); return ZipReturn.ZipErrorOpeningFile; } ZipOpen = ZipOpenType.OpenRead; if (!readHeaders) return ZipReturn.ZipGood; try { ZipReturn zRet = FindEndOfCentralDirSignature(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } long endOfCentralDir = _zipFs.Position; zRet = EndOfCentralDirRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } // check if this is a ZIP64 zip and if it is read the Zip64 End Of Central Dir Info if (_centerDirStart == 0xffffffff || _centerDirSize == 0xffffffff || _localFilesCount == 0xffff) { _zip64 = true; _zipFs.Position = endOfCentralDir - 20; zRet = Zip64EndOfCentralDirectoryLocatorRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } _zipFs.Position = (long)_endOfCenterDir64; zRet = Zip64EndOfCentralDirRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } } bool trrntzip = false; // check if the ZIP has a valid TorrentZip file comment if (_fileComment.Length == 22) { if (GetString(_fileComment).Substring(0, 14) == "TORRENTZIPPED-") { CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true); byte[] buffer = new byte[_centerDirSize]; _zipFs.Position = (long)_centerDirStart; crcCs.Read(buffer, 0, (int)_centerDirSize); crcCs.Flush(); crcCs.Close(); uint r = (uint)crcCs.Crc; crcCs.Dispose(); string tcrc = GetString(_fileComment).Substring(14, 8); string zcrc = r.ToString("X8"); if (String.Compare(tcrc, zcrc, StringComparison.Ordinal) == 0) trrntzip = true; } } // now read the central directory _zipFs.Position = (long)_centerDirStart; _localFiles.Clear(); _localFiles.Capacity = (int)_localFilesCount; for (int i = 0; i < _localFilesCount; i++) { LocalFile lc = new LocalFile(_zipFs); zRet = lc.CenteralDirectoryRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } _zip64 |= lc.Zip64; _localFiles.Add(lc); } for (int i = 0; i < _localFilesCount; i++) { zRet = _localFiles[i].LocalFileHeaderRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } trrntzip &= _localFiles[i].TrrntZip; } // check trrntzip file order if (trrntzip) for (int i = 0; i < _localFilesCount - 1; i++) { if (TrrntZipStringCompare(_localFiles[i].FileName, _localFiles[i + 1].FileName) < 0) continue; trrntzip = false; break; } // check trrntzip directories if (trrntzip) for (int i = 0; i < _localFilesCount - 1; i++) { // see if we found a directory string filename0 = _localFiles[i].FileName; if (filename0.Substring(filename0.Length - 1, 1) != "/") continue; // see if the next file is in that directory string filename1 = _localFiles[i + 1].FileName; if (filename1.Length <= filename0.Length) continue; if (TrrntZipStringCompare(filename0, filename1.Substring(0, filename0.Length)) != 0) continue; // if we found a file in the directory then we do not need the directory entry trrntzip = false; break; } if (trrntzip) _pZipStatus |= ZipStatus.TrrntZip; return ZipReturn.ZipGood; } catch { ZipFileClose(); return ZipReturn.ZipErrorReadingFile; } }
//public List<FTPFile> GetFTPFiles() //{ // try // { // FTPFile[] filesDirectories = ftp.GetFileInfos(); // List<FTPFile> files = new List<FTPFile>(); // foreach (FTPFile f in filesDirectories) // { // if (!f.Dir) // files.Add(f); // } // return files; // } // catch (Exception e) // { // throw e; // } //} public Dictionary<string, FileInfo> GetLocalFiles() { string[] filePaths = Directory.GetFiles(localRootDir); Dictionary<string, FileInfo> fileList = new Dictionary<string, FileInfo>(); foreach (string filePath in filePaths) { LocalFile file = new LocalFile(); FileInfo fileInfo = new FileInfo(filePath); fileList.Add(fileInfo.Name, fileInfo); } return fileList; }
public LocalFile(LocalFile file, string displayName) : this() { FullPath = file.FullPath; RelativePath = file.RelativePath; FileName = file.FileName; NSDictionary attrs = file.DisplayText.fontAttributesInRange(new NSRange(0, 1)); DisplayText = NSAttributedString.Create(displayName, attrs); DisplayText.retain(); }
private void DoGetCandidates(IEnumerable<IGetFiles> roots) { var candidates = new List<LocalFile>(); var pool = NSAutoreleasePool.Create(); try { // Get all the files within the directories being edited. var threaded = new List<ThreadedFile>(); var files = new List<string>(); var colors = new List<NSColor>(); foreach (IGetFiles getter in roots) { files.Clear(); colors.Clear(); getter.GetFiles(files, colors); for (int i = 0; i < files.Count; ++i) { string relativePath = files[i].Substring(Path.GetDirectoryName(getter.Path).Length); // use the path up to, and including, the directory being edited threaded.Add(new ThreadedFile(files[i], relativePath, colors[i])); } } // Use a reversed path for the name for any entries with duplicate names. candidates = (from t in threaded select new LocalFile(t)).ToList(); candidates.Sort((lhs, rhs) => lhs.FileName.CompareTo(rhs.FileName)); for (int i = 0; i < candidates.Count - 1; ++i) { string name = candidates[i].FileName; if (candidates[i + 1].FileName == name) { for (int j = i; j < candidates.Count && candidates[j].FileName == name; ++j) { LocalFile f = candidates[j]; candidates[j] = new LocalFile(f, f.RelativePath.ReversePath()); f.DisplayText.release(); } } } } catch (Exception e) { string err = string.Format("Error getting local files: {0}", e.Message); NSApplication.sharedApplication().BeginInvoke(() => DoShowError(err)); } NSApplication.sharedApplication().BeginInvoke(() => DoRefresh(candidates)); pool.release(); }
private void SendLocalFile(string localFile, string requestRange, string connection) { _mLocalFile = new LocalFile(localFile); long startRange; var responseStr = BuildResponse(requestRange, connection, out startRange); _mLocalFile.FileStream.Seek(startRange, SeekOrigin.Begin); try { ClientSocket.BeginSend(Encoding.ASCII.GetBytes(responseStr), 0, responseStr.Length, SocketFlags.None, OnLocalFileSent, ClientSocket); } catch { _mLocalFile.FileStream.Close(); Dispose(); } }
public static LocalFile getFileInfo(string filePath) { LocalFile currentFile = new LocalFile(); try { int indexOfDot = filePath.LastIndexOf('.'); currentFile.type = filePath.Substring(indexOfDot); } catch (Exception e) { currentFile.type = ""; } try { int indexOfSlash = filePath.LastIndexOf('\\'); currentFile.name = filePath.Substring(indexOfSlash+1); } catch (Exception e) { currentFile.name = filePath; } currentFile.size = new FileInfo(filePath).Length.ToString(); try { int indexOfLastSlash = filePath.LastIndexOf('\\'); currentFile.parentFolder = filePath.Substring(0,indexOfLastSlash); int indexOfSecondSlash = currentFile.parentFolder.LastIndexOf('\\'); currentFile.parentFolder = currentFile.parentFolder.Substring(indexOfSecondSlash+1); } catch (Exception e) { currentFile.name = filePath; } currentFile.location = filePath; if (currentFile.name.ToLower().Contains("thumbs.db") || currentFile.name.ToLower().Contains("albumart") || currentFile.name.ToLower().Contains("desktop.ini") || currentFile.name.ToLower().Contains("folder.jpg")) { //Utils.writeLog("getFileInfo: Ignored file " + filePath); return null; } if ((currentFile.hash = GenerateHash(filePath)) == null) { Utils.writeLog("getFileInfo: Unable to generate hash of file " + filePath); return null; } /* List<string> propertyHeaders = new List<string>(); Dictionary<string, string> properties = new Dictionary<string, string>(); Shell32.Shell shell = new Shell32.Shell(); Shell32.Folder locationFolder; locationFolder = shell.NameSpace(filePath); String s = ""; for (int i = 0; i < short.MaxValue; i++) { string header = locationFolder.GetDetailsOf(null, i); if (String.IsNullOrEmpty(header)) break; propertyHeaders.Add(header); } foreach (Shell32.FolderItem item in locationFolder.Items()) { for (int i = 0; i < propertyHeaders.Count; i++) { properties.Add(propertyHeaders[i], locationFolder.GetDetailsOf(item, i)); s = s + propertyHeaders[i] + " : " + locationFolder.GetDetailsOf(item, i) + "\n"; } } MessageBox.Show(s); */ currentFile.computeKeywords(); return currentFile; }