# How to decompress .xz files/ How to use streams?

**URL:** https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307
**Category:** General Usage
**Tags:** question, package, codecxz
**Created:** [August 25, 2022, 7:31am UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307 "2022-08-25T07:31:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 25, 2022, 7:31am UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307/1 "2022-08-25T07:31:52Z")

</div>

I have the following code for unpacking .xz compressed files:

```julia
using CodecXz

function decompress(in, out)
    stream = open(in)
    output = open(out,"w")
    for line in eachline(XzDecompressorStream(stream))
        println(output, line)
    end
    close(stream)
    close(output)
end

```

This does not always work due to the use of eachline and println…

How can I do this in a more generic form?

@aplavin suggested to use `read(XzDecompressorStream(stream))`, but I do not want to do that because it creates a huge vector in RAM. I would prefer to do the decompression in chunks.

How can I do that in Julia?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 25, 2022, 7:41am UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307/2 "2022-08-25T07:41:05Z")

</div>

I guess my main problem is that I don’t fully understand how to use streams.

How can I for example copy a binary file using an input and an output stream?

```julia
function copyfile(input_file, output_file)
    in_stream = open(input_file)
    out_stream = open(output_file,"w")

    # What to put here?

    close(in_stream)
    close(out_stream)
end

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 25, 2022, 7:51am UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307/3 "2022-08-25T07:51:36Z")

</div>

```julia
function copyfile(input_file, output_file)
    in_stream = open(input_file)
    out_stream = open(output_file,"w")
    
    while !eof(in_stream)
        write(out_stream, read(in_stream, 10000))
    end
    
    close(in_stream)
    close(out_stream)
end

```

Something like that (maybe even exactly that) should work for your original question as well.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [August 25, 2022, 8:55am UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307/4 "2022-08-25T08:55:16Z")

</div>

Or simply

```julia
write(out_stream, in_stream)

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 25, 2022, 9:46am UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307/5 "2022-08-25T09:46:18Z")

</div>

Not working:

```julia
function decompress1(in, out)
    stream = open(in)
    output = open(out,"w")
    while !eof(stream)
        write(output, read(XzDecompressorStream(stream), 10000))
    end
    close(stream)
    close(output)
end

```

It fails with:

```julia
ERROR: LoadError: lzma error: code = 7
Stacktrace:
  [1] changemode!(stream::TranscodingStreams.TranscodingStream{XzDecompressor, IOStream}, newmode::Symbol)
    @ TranscodingStreams ~/.julia/packages/TranscodingStreams/TsaT2/src/stream.jl:724
  [2] callprocess(stream::TranscodingStreams.TranscodingStream{XzDecompressor, IOStream}, inbuf::TranscodingStreams.Buffer, outbuf::TranscodingStreams.Buffer)
    @ TranscodingStreams ~/.julia/packages/TranscodingStreams/TsaT2/src/stream.jl:654
  [3] fillbuffer(stream::TranscodingStreams.TranscodingStream{XzDecompressor, IOStream}; eager::Bool)
    @ TranscodingStreams ~/.julia/packages/TranscodingStreams/TsaT2/src/stream.jl:582
  [4] fillbuffer
    @ ~/.julia/packages/TranscodingStreams/TsaT2/src/stream.jl:568 [inlined]
  [5] eof(stream::TranscodingStreams.TranscodingStream{XzDecompressor, IOStream})
    @ TranscodingStreams ~/.julia/packages/TranscodingStreams/TsaT2/src/stream.jl:191
  [6] readbytes!(stream::TranscodingStreams.TranscodingStream{XzDecompressor, IOStream}, b::Vector{UInt8}, nb::Int64)
    @ TranscodingStreams ~/.julia/packages/TranscodingStreams/TsaT2/src/stream.jl:375
  [7] read(s::TranscodingStreams.TranscodingStream{XzDecompressor, IOStream}, nb::Int64)
    @ Base ./io.jl:1000
  [8] decompress(in::String, out::String)
    @ Main ~/repos/CanAnalyzer/src/process_log.jl:97
  [9] main(logfile_name::String)
    @ Main ~/repos/CanAnalyzer/src/process_log.jl:500
 [10] top-level scope
    @ ~/repos/CanAnalyzer/src/process_log.jl:595
 [11] include(fname::String)
    @ Base.MainInclude ./client.jl:476
 [12] top-level scope
    @ REPL[1]:1
in expression starting at ...

```

Working:

```julia
function decompress(in, out)
    stream = open(in)
    output = open(out,"w")
    write(output, read(XzDecompressorStream(stream)))
    close(stream)
    close(output)
end

```

So I have a working solution, but it allocates the full size of the output file in RAM…

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [August 25, 2022, 10:27am UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307/6 "2022-08-25T10:27:25Z")

</div>

```julia
function decompress(in, out)
    stream = open(in)
    output = open(out,"w")
    write(output, XzDecompressorStream(stream))
    close(stream)
    close(output)
end

```

Also you should use the do-block version of `open` to ensure the streams are closed even if an error occurs.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [September 3, 2022, 11:00pm UTC](https://discourse.julialang.org/t/how-to-decompress-xz-files-how-to-use-streams/86307/7 "2022-09-03T23:00:32Z")

</div>

Is this better?

```julia
function decompress(input, output)
    open(input) do inp
        open(output,"w") do out
            write(out, read(XzDecompressorStream(inp)))
        end
    end
end

```
