# Is it possible to compress a string the way a text file can be compressed?

**URL:** <https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422>\
**Category:** Help & Support\
**Created:** [February 24, 2023, 6:30pm UTC](https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422 "2023-02-24T18:30:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 24, 2023, 6:30pm UTC](https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422/1 "2023-02-24T18:30:50Z")

</div>

Is it possible to compress a string the way a text file can be compressed?

If yes, how can it be done using Crystal?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 24, 2023, 6:37pm UTC](https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422/2 "2023-02-24T18:37:17Z")

</div>

I think could essentially just do something like:

```auto
require "compress/gzip"
 
compressed_string = String.build do |io|
  Compress::Gzip::Writer.open(io) do |gzip|
    gzip << "Compress me"
  end
end

```

Tho what’s your use case for wanting to do this?

---

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 24, 2023, 6:57pm UTC](https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422/3 "2023-02-24T18:57:47Z")

</div>

Thanks a lot.

It could come in handy to reduce the size of my (very crude) prototype of virtual file system: [GitHub - serge-hulne/bakerstreet: Virtual file system for servers written in Crystal lang (aimed at embedding text resources (.html, .css,. js) in app)](https://github.com/serge-hulne/bakerstreet)

The idea behind this vfs is : shipping Webview-based apps as one single file (the html, css, js, svg resources being embedded in the executable).

The two obvious candidates for vfs under Crystal don’t work at the moment under Windows, yet I want my system to be portable between macOS, Linux and Windows, so I resorted to write my own vfs.

PS: If I may : how do you carry out the reverse operation (decompression of the compressed string) ?

---

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 24, 2023, 7:05pm UTC](https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422/4 "2023-02-24T19:05:52Z")

</div>

The endgame is : I’m working on a clone of Wails which uses Crystal in place of Go.

> **[The Wails Project | Wails](https://wails.io/)**
>
> Build beautiful cross-platform applications using Go

I expect my first 100% cross-platform version to be ready very soon.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 24, 2023, 7:21pm UTC](https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422/5 "2023-02-24T19:21:56Z")

</div>

> [@serge-hulne](#):
>
> PS: If I may : how do you carry out the reverse operation (decompression of the compressed string) ?

Probably something like:

```auto
io = IO::Memory.new compressed_string

string = Compress::Gzip::Reader.open(io) do |gzip|
  gzip.gets_to_end
end

```

---

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 24, 2023, 7:24pm UTC](https://forum.crystal-lang.org/t/is-it-possible-to-compress-a-string-the-way-a-text-file-can-be-compressed/5422/6 "2023-02-24T19:24:14Z")

</div>

Thanks!
