/// <summary> /// Seeks on a file pointer. /// </summary> /// <param name="handle">A file stream resource.</param> /// <param name="offset">The number of bytes to seek.</param> /// <param name="whence">The position in stream to seek from. /// May be one of the <see cref="SeekOptions"/> flags.</param> /// <returns>Upon success, returns 0; otherwise, returns -1. /// Note that seeking past EOF is not considered an error.</returns> public static int fseek(PhpResource handle, int offset, int whence = SEEK_SET) { PhpStream stream = PhpStream.GetValid(handle); if (stream == null) { return(-1); } return(stream.Seek(offset, (SeekOrigin)whence) ? 0 : -1); }
/// <summary> /// Adds a file to a ZIP archive from a given path. /// </summary> /// <param name="ctx">Current runtime context.</param> /// <param name="filename">The path to the file to add.</param> /// <param name="localname">If supplied, this is the local name inside the ZIP archive that will override the filename.</param> /// <param name="start">For partial copy, start position.</param> /// <param name="length">For partial copy, length to be copied, if 0 or -1 the whole file (starting from <paramref name="start"/>) is used.</param> /// <returns>TRUE on success or FALSE on failure.</returns> public bool addFile(Context ctx, string filename, string localname = null, int start = 0, int length = 0) { if (!CheckInitialized()) { return(false); } ZipArchiveEntry entry = null; try { entry = CreateEntryIfNotExists(localname ?? Path.GetFileName(filename)); entry.LastWriteTime = File.GetLastWriteTime(FileSystemUtils.AbsolutePath(ctx, filename)); using (var entryStream = entry.Open()) using (PhpStream handle = PhpStream.Open(ctx, filename, "r", StreamOpenOptions.Empty)) { if (start != 0) { handle.Seek(start, SeekOrigin.Begin); } if (length == 0 || length == -1) { handle.RawStream.CopyTo(entryStream); } else { // We need to copy the contents manually if the length was specified var buffer = new byte[Math.Min(length, PhpStream.DefaultBufferSize)]; int copied = 0; while (copied < length) { int lastCopied = handle.RawStream.Read(buffer, 0, Math.Min(buffer.Length, length - copied)); entryStream.Write(buffer, 0, lastCopied); copied += lastCopied; } } } return(true); } catch (System.Exception e) { PhpException.Throw(PhpError.Warning, e.Message); entry?.Delete(); return(false); } }