Creating gz file using gzip vs. vim


New Member
Nov 26, 2021
Reaction score

Firstly, I would like to say "hello". It is my first post on this forum :)

I have been struggling with some issue since few days.

I am a C++ programmer and I am developing some C++ app which uses rapidjson library to read file_name.json.gz file.
The application crashes in some place when I create the gz file in the following way:
vi file_name.json.gz

BUT. When i use the following commands, there is no crash.
vi file_name.json
gzip file_name.json

Does somebody know what is a difference between creating gz file using vi versus creating using gzip?



Well-Known Member
Apr 25, 2017
Reaction score
The problem is - vi/vim is a text editor, it works only on text based files.
.gz is a compressed format.

As you’ve found, you need to create file-name.json in your text editor and then gzip it. That is the normal, expected way of doing things.
Create your file in a text editor, then gzip it.

If you create a file called file-name.json.gz in vim, it simply creates a flat, text file called file-name.json.gz. But it is just a text file, there is no compression involved, so it isn’t a valid .gz file.
So that is why your program is crashing.
Because the .gz file you created in vi is not a valid .gz file.

I’m not familiar with the rapidjson library, but there is probably a way to gracefully handle errors like this, to prevent a crash too.

I’m not sure if rapidjson implements any kind of exception handling mechanism, or whether there is any way you can use exceptions in your program to catch and handle errors from rapidjson. But it should be possible to prevent a crash from occurring if rapidjson receives an invalid file.

Usually .json files are returned by web services, which create a .json file on the fly, server-side, gzip’s it and then returns it to the client who sent the request.

So I imagine this .json file of yours is test data for your program?
Or are you using gzipped .json files as config files for your program? Or something else?

If you have a lot of these .json files, then as part of your build process, you could set up a post-build step which fires off a script that will find all .json files and gzip them.
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online