Module: ZstdRubyNative

Defined in:
ext/zstd_ruby/zstd_ruby.c

Class Method Summary collapse

Class Method Details

.compress_file_native(input_path, output_path, level) ⇒ Boolean

Compress a file using zstd

Parameters:

  • input_path (String)

    path to input file

  • output_path (String)

    path to output file

  • level (Integer)

    compression level (1-22)

Returns:

  • (Boolean)

    true on success



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/zstd_ruby/zstd_ruby.c', line 19

static VALUE zstd_compress_file(VALUE self, VALUE input_path, VALUE output_path, VALUE level) {
    Check_Type(input_path, T_STRING);
    Check_Type(output_path, T_STRING);
    Check_Type(level, T_FIXNUM);

    const char *input_path_str = StringValueCStr(input_path);
    const char *output_path_str = StringValueCStr(output_path);
    int compression_level = FIX2INT(level);

    FILE *input_file = fopen(input_path_str, "rb");
    if (!input_file) {
        rb_raise(rb_eRuntimeError, "Cannot open input file: %s", input_path_str);
        return Qfalse;
    }

    FILE *output_file = fopen(output_path_str, "wb");
    if (!output_file) {
        fclose(input_file);
        rb_raise(rb_eRuntimeError, "Cannot open output file: %s", output_path_str);
        return Qfalse;
    }

    // Create compression context
    ZSTD_CCtx *cctx = ZSTD_createCCtx();
    if (!cctx) {
        fclose(input_file);
        fclose(output_file);
        rb_raise(rb_eRuntimeError, "Failed to create compression context");
        return Qfalse;
    }

    // Set compression level
    ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compression_level);

    // Allocate buffers
    void *input_buffer = malloc(BUFFER_SIZE);
    size_t output_buffer_size = ZSTD_compressBound(BUFFER_SIZE);
    void *output_buffer = malloc(output_buffer_size);

    if (!input_buffer || !output_buffer) {
        free(input_buffer);
        free(output_buffer);
        ZSTD_freeCCtx(cctx);
        fclose(input_file);
        fclose(output_file);
        rb_raise(rb_eRuntimeError, "Memory allocation failed");
        return Qfalse;
    }

    // Read and compress file in chunks
    size_t bytes_read;

    while ((bytes_read = fread(input_buffer, 1, BUFFER_SIZE, input_file)) > 0) {
        ZSTD_inBuffer input = {input_buffer, bytes_read, 0};

        // Process all input
        while (input.pos < input.size) {
            ZSTD_outBuffer output = {output_buffer, output_buffer_size, 0};
            size_t remaining = ZSTD_compressStream2(cctx, &output, &input, ZSTD_e_continue);

            if (ZSTD_isError(remaining)) {
                free(input_buffer);
                free(output_buffer);
                ZSTD_freeCCtx(cctx);
                fclose(input_file);
                fclose(output_file);
                rb_raise(rb_eRuntimeError, "Compression failed: %s", ZSTD_getErrorName(remaining));
                return Qfalse;
            }

            fwrite(output_buffer, 1, output.pos, output_file);
        }
    }

    // Finalize the compression stream
    ZSTD_inBuffer empty_input = {NULL, 0, 0};
    int finished = 0;
    while (!finished) {
        ZSTD_outBuffer output = {output_buffer, output_buffer_size, 0};
        size_t remaining = ZSTD_compressStream2(cctx, &output, &empty_input, ZSTD_e_end);

        if (ZSTD_isError(remaining)) {
            free(input_buffer);
            free(output_buffer);
            ZSTD_freeCCtx(cctx);
            fclose(input_file);
            fclose(output_file);
            rb_raise(rb_eRuntimeError, "Compression finalization failed: %s", ZSTD_getErrorName(remaining));
            return Qfalse;
        }

        fwrite(output_buffer, 1, output.pos, output_file);
        finished = (remaining == 0);
    }

    // Cleanup
    free(input_buffer);
    free(output_buffer);
    ZSTD_freeCCtx(cctx);
    fclose(input_file);
    fclose(output_file);

    return Qtrue;
}

.decompress_file_native(input_path, output_path) ⇒ Boolean

Decompress a file using zstd

Parameters:

  • input_path (String)

    path to compressed input file

  • output_path (String)

    path to output file

Returns:

  • (Boolean)

    true on success



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/zstd_ruby/zstd_ruby.c', line 131

static VALUE zstd_decompress_file(VALUE self, VALUE input_path, VALUE output_path) {
    Check_Type(input_path, T_STRING);
    Check_Type(output_path, T_STRING);

    const char *input_path_str = StringValueCStr(input_path);
    const char *output_path_str = StringValueCStr(output_path);

    FILE *input_file = fopen(input_path_str, "rb");
    if (!input_file) {
        rb_raise(rb_eRuntimeError, "Cannot open input file: %s", input_path_str);
        return Qfalse;
    }

    FILE *output_file = fopen(output_path_str, "wb");
    if (!output_file) {
        fclose(input_file);
        rb_raise(rb_eRuntimeError, "Cannot open output file: %s", output_path_str);
        return Qfalse;
    }

    // Create decompression context
    ZSTD_DCtx *dctx = ZSTD_createDCtx();
    if (!dctx) {
        fclose(input_file);
        fclose(output_file);
        rb_raise(rb_eRuntimeError, "Failed to create decompression context");
        return Qfalse;
    }

    // Allocate buffers
    size_t input_buffer_size = ZSTD_DStreamInSize();
    void *input_buffer = malloc(input_buffer_size);
    size_t output_buffer_size = ZSTD_DStreamOutSize();
    void *output_buffer = malloc(output_buffer_size);

    if (!input_buffer || !output_buffer) {
        free(input_buffer);
        free(output_buffer);
        ZSTD_freeDCtx(dctx);
        fclose(input_file);
        fclose(output_file);
        rb_raise(rb_eRuntimeError, "Memory allocation failed");
        return Qfalse;
    }

    // Read and decompress file
    size_t bytes_read;
    size_t last_result = 0;

    while ((bytes_read = fread(input_buffer, 1, input_buffer_size, input_file)) > 0) {
        ZSTD_inBuffer input = {input_buffer, bytes_read, 0};

        while (input.pos < input.size) {
            ZSTD_outBuffer output = {output_buffer, output_buffer_size, 0};

            size_t result = ZSTD_decompressStream(dctx, &output, &input);

            if (ZSTD_isError(result)) {
                free(input_buffer);
                free(output_buffer);
                ZSTD_freeDCtx(dctx);
                fclose(input_file);
                fclose(output_file);
                rb_raise(rb_eRuntimeError, "Decompression failed: %s", ZSTD_getErrorName(result));
                return Qfalse;
            }

            fwrite(output_buffer, 1, output.pos, output_file);
            last_result = result;
        }
    }

    // Cleanup
    free(input_buffer);
    free(output_buffer);
    ZSTD_freeDCtx(dctx);
    fclose(input_file);
    fclose(output_file);

    return Qtrue;
}