使用流式函数解决v语言zstd程序解压缩失败问题

上文的简易压缩/解压程序在解压缩较大文件时出错,输出到文件为0字节。

bash 复制代码
请输入要解压的.zst文件路径: vzstd.zst
请输入解压后文件保存路径: var.txt
正在解压 vzstd.zst...
解压完成!
压缩文件: vzstd.zst (88089043 字节)
解压文件: var.txt (0 字节)        <---------------no data in output file
耗时: 0.43 秒

我在github上提出此问题

kbkpbot 回答如下

os.write_bytes(output_file, decompressed_data)
For small zstd file, you can use compress()/decompress() directly.

For large zstd file, because V internal array size limit(it use int which has a limit), you can't use a supper large buffer now, I suggest use a stream mode compress/decompress.

并提供了解决此问题的具体改用流式函数实现,

c 复制代码
const buf_in_size = 1024 * 1024
const buf_out_size = 1024 * 1024

fn decompress_file_fun(fname string, oname string, params zstd.DecompressParams) ! {
	mut fin := os.open_file(fname, 'rb')!
	mut fout := os.open_file(oname, 'wb')!
	defer {
		fin.close()
		fout.close()
	}

	mut buf_in := []u8{len: buf_in_size}
	mut buf_out := []u8{len: buf_out_size}

	mut dctx := zstd.new_dctx(params)!
	defer {
		dctx.free_dctx()
	}

	mut input := &zstd.InBuffer{
		src:  buf_in.data
		size: 0
		pos:  0
	}
	mut output := &zstd.OutBuffer{
		dst:  buf_out.data
		size: 0
		pos:  0
	}

	mut last_ret := usize(0)
	for {
		read_len := fin.read(mut buf_in)!
		input.src = buf_in.data
		input.size = usize(read_len)
		input.pos = 0
		for input.pos < input.size {
			output.dst = buf_out.data
			output.size = buf_out_size
			output.pos = 0
			ret := dctx.decompress_stream(output, input)!
			fout.write(buf_out[..output.pos])!
			last_ret = ret
		}
		if read_len < buf_in.len {
			break
		}
	}
	if last_ret != 0 {
		/* The last return value from DecompressStream did not end on a
         * frame, but we reached the end of the file! We assume this is an
         * error, and the input was truncated.
         */
		return error('EOF before end of stream: ${last_ret}')
	}
}

将此函数添加到源代码,并在decompress_file函数中用如下语句调用代替原有解压缩部分代码。

c 复制代码
    println("正在解压 ${input_file}...")
    start_time := time.now()

    decompress_file_fun(input_file, output_file)or {} //or {}必须,否则编译出错

    duration := time.since(start_time).seconds()

这样就可以解压缩完成了

c 复制代码
v -prod vzstd2.v

./vzstd2
ZSTD 压缩/解压工具
1. 压缩文件
2. 解压文件
3. 退出程序
请选择操作 (1-3): 2
请输入要解压的.zst文件路径: vzstd2.zst
请输入解压后文件保存路径: ccc.txt
正在解压 vzstd2.zst...
解压完成!
压缩文件: vzstd2.zst (88089043 字节)
解压文件: ccc.txt (106046919 字节)
耗时: 0.78 秒
相关推荐
霉运全滚蛋好运围着转4 小时前
启动 Taro 4 项目报错:Error: The specified module could not be found.
前端
cxxcode4 小时前
前端模块化发展
前端
不务正业的前端学徒4 小时前
docker+nginx部署
前端
不务正业的前端学徒4 小时前
webpack/vite配置
前端
hhcccchh4 小时前
学习vue第八天 Vue3 模板语法和内置指令 - 简单入门
前端·vue.js·学习
yyf198905254 小时前
Vue 框架相关中文文献
前端·javascript·vue.js
粥里有勺糖4 小时前
开发一个美观的 VitePress 图片预览插件
前端·vue.js·vitepress
陟上青云5 小时前
一篇文章带你搞懂原型和原型链
前端
我的写法有点潮5 小时前
推荐几个国外比较流行的UI库(上)
前端·javascript·css
鹏多多5 小时前
jsx/tsx使用cssModule和typescript-plugin-css-modules
前端·vue.js·react.js