Code Monkey home page Code Monkey logo

lua-fs-utils's Introduction

Lua File System Utils

The fs module is a set of functions that provide various file system related functionality, such as finding, reading, writing and streaming files.

Functions

find(path, name)

The find function searches for a file with the given name in the directory specified by path. It returns the full path to the file if it is found, otherwise it returns nil.

local path = fs.find('/home/user', 'example.txt')
print(path) -- '/home/user/example.txt'

fileName(path)

The fileName function takes a file path as an argument and returns the file name from the path.

local fileName = fs.fileName('/home/user/example.txt')
print(fileName) -- 'example.txt'

fileExtension(path)

The fileExtension function takes a file path as an argument and returns the file extension from the path.

local fileExtension = fs.fileExtension('/home/user/example.txt')
print(fileExtension) -- 'txt'

readAsText(path)

The readAsText function reads the file at the specified path and returns its content as a string.

local fileContent = fs.readAsText('/home/user/example.txt')
print(fileContent) -- 'This is an example text file.'

readAsJson(path)

The readAsJson function reads the file at the specified path, converts it to a JSON object, and returns the object.

local fileContent = fs.readAsJson('/home/user/example.json')
print(fileContent) -- { name: "example", value: 42 }

read(path, binary)

The read function reads the file at the specified path and returns its content as an array of bytes. If the binary parameter is set to true, the file will be read in binary mode, otherwise it will be read in text mode.

Reading binary file:

local fileContent = fs.read('/home/user/example.bin', true)
print(fileContent) -- { 0x01, 0x02, 0x03, ... }

Reading file as text, similar to readAsText:

local fileContent = fs.read('/home/user/example.txt', false)
print(fileContent) -- 'This is an example text file.'

write(path, content)

The write function writes the content to the file at the specified path.

fs.write('/home/user/example.txt', 'This is new content')

exists(path)

The exists function checks if the file or directory at the specified path exists and returns true if it does, otherwise false.

local exists = fs.exists('/home/user/example.txt')
print(exists) -- true

filesCount(path)

The filesCount function returns the number of files in a given folder path.

local filesCount = fs.filesCount('/home/user')
print(filesCount) -- 3

matchSignature(path, signature)

The matchSignature function accepts a path โ€” a path to the file and signature - array of bytes. It checks if the path is file, and it exists, then checks if file content starts with 'signature'. return true if matched, otherwise

local match = fs.matchSignature('/home/user/example.txt', { 'T', 'h', 'i', 's', ' ', 'i', 's' })
print(match) -- true

stream(path, chunkSize)

Streams file to stdout or, if running under openresty to the user, the chunkSize is optional

fs.stream('/home/user/example.mp4',8192)

lua-fs-utils's People

Contributors

skitsanos avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.