Node.js从文件中删除前N个字节

问题描述

如何从二进制文件的开头删除删除|修剪)N个字节而不将其加载到内存中?

我们有fs.ftruncate(fd,len,callback),它从文件末尾切出字节(如果更大)。

如何在不读取内存中文件的情况下在Node.js中从头开始削减字节或从头开始修剪?

我需要类似truncateFromBeggining(fd,callback)removeBytes(fd,N,callback)的东西。

如果不可能,使用文件流最快的方法是什么?

在大多数文件系统上,您不能从文件的开头或中间“切出”一部分,而只能在结尾处截断。

我想着上面的几点,我们可能必须打开输入文件流,以寻找第N个字节之后的内容,并将其余字节pipe cvs server: cannot lock [filename]输出文件流中。

解决方法

您要的是OS文件系统操作:能够从文件的开头删除一些字节,而无需重写文件。

您要的文件系统操作不存在,至少在Linux / FreeBSD / MacOS / Windows中如此。

如果您的程序是文件的唯一用户,并且它适合RAM,那么最好的选择是将整个内容读入RAM,然后重新打开文件进行写入,然后写出要保留的部分。

或者您可以创建一个新文件。假设您的输入文件名为q。然后,您将创建一个名为new_q的文件,并附加一个流。您会将所需的内容通过管道传输到新文件。然后,将unlink (delete)输入文件qrename输出文件new_qq

小心:当没有名为q的文件可用时,此取消链接/重命名操作将在很短的时间内创建。因此,如果其他某个程序尝试打开它却没有找到它,则应再次尝试几次。

如果要创建排队方案,则可以考虑使用其他方案来保存队列数据。此文件的读取/重写/取消链接/重命名顺序有很多方法可以使您在重负载下出错。 (请问我,我知道当您有几个小时的空闲时间时;-) redis值得一看。

,

我决定解决bash中的问题。

该脚本会首先截断temp文件夹中的文件,然后将它们移回原始文件夹。

截断是通过tail完成的:

tail --bytes="$max_size" "$from_file" > "$to_file"

完整脚本:

#!/bin/bash

declare -r store="/my/data/store"
declare -r temp="/my/data/temp"
declare -r max_size=$(( 200000 * 24 ))

or_exit() {
    local exit_status=$?
    local message=$*

    if [ $exit_status -gt 0 ]
    then
        echo "$(date '+%F %T') [$(basename "$0" .sh)] [ERROR] $message" >&2
        exit $exit_status
    fi
}

# Checks if there are any files in 'temp'. It should be empty.
! ls "$temp/"* &> '/dev/null'
    or_exit 'Temp folder is not empty'

# Loops over all the files in 'store'
for file_path in "$store/"*
do
    # Trim bigger then 'max_size' files from 'store' to 'temp'
    if [ "$( stat --format=%s "$file_path" )" -gt "$max_size" ]
    then
        # Truncates the file to the temp folder
        tail --bytes="$max_size" "$file_path" > "$temp/$(basename "$file_path")"
            or_exit "Cannot tail: $file_path"
    fi
done
unset -v file_path

# If there are files in 'temp',move all of them back to 'store'
if ls "$temp/"* &> '/dev/null'
then
    # Moves all the truncated files back to the store
    mv "$temp/"* "$store/"
        or_exit 'Cannot move files from temp to store'
fi