user34537
user34537

Reputation:

Not able to use 7-Zip to compress stdin and output with stdout?

I get the error "Not implemented".

I want to compress a file using 7-Zip via stdin then take the data via stdout and do more conversions with my application. In the man page it shows this example:

% echo foo | 7z a dummy -tgzip -si -so > /dev/null

I am using Windows and C#.

Results:

7-Zip 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Creating archive StdOut

System error:
Not implemented

Code:

public static byte[] a7zipBuf(byte[] b)
{
    string line;
    var p = new Process();
    line = string.Format("a dummy -t7z -si -so ");
    p.StartInfo.Arguments = line;
    p.StartInfo.FileName = @"C:\Program Files\7-Zip\7z.exe";
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardInput = true;

    p.Start();

    p.StandardInput.BaseStream.Write(b, 0, b.Length);
    p.StandardInput.Close();
    Console.Write(p.StandardError.ReadToEnd());
    //Console.Write(p.StandardOutput.ReadToEnd());

    return p.StandardOutput.BaseStream.ReadFully();
}

Is there another simple way to read the file into memory?

Right now I can 1) write to a temporary file and read (easy and can copy/paste some code) 2) use a file pipe (medium? I have never done it) 3) Something else.

Upvotes: 8

Views: 12731

Answers (5)

dessalines
dessalines

Reputation: 7372

The simplest way is to use xz, which by default uses the same compression algorithm as 7-zip, with the -T0 param, which makes xz multithreaded scaling to the number of your CPU cores automatically, and as fast as 7-zip.

some_command | xz -T0 > some_file.xz

Upvotes: 1

Xen2050
Xen2050

Reputation: 2500

Here's some info from Igor Pavlov (7z's author), in a thread about "7zip as a drop-in replacement for gzip/bzip2?"

The suggestion was to basically use 7z as an xz surrogate. Just using xz should work, but it may not be multi-threaded (and 7z may be).

While attempting to use 7z as in:

somecommand | 7zr a -si -so | nc -q 2 1.2.3.4 5678

Igor Pavlov says:

7z a a.7z -so
and
7z e a.7z -si
can not be implemeted. since .7z format requires "Seek" operation.

Use xz format instead:
7z a a.xz file
it must support all modes.

And

7-Zip thinks that it needs archive name.
So you can specify some archive name like a.xz
or
specify -an switch.

The eventual solution was:

cat foo.txt | 7za a -an -txz -bd -si -so | dd of=foo.xz

A bug report suggests this should be in the help:

The current version of 7-Zip support reading of archives from stdin only for xz, lzma, tar, gzip and bzip2 archives, and adding files from stdin only for 7z, xz, gzip and bzip2 archives.

Upvotes: 3

Brian
Brian

Reputation: 7146

I ran into a similar problem when piping stdout into 7zip

Instead of invoking the command from Process directly, I write the command to a batch file and then run the batch file. It's a hack, but it does work.

Upvotes: 0

RobV
RobV

Reputation: 28646

You might want to try out something like SevenZipSharp http://www.codeplex.com/sevenzipsharp, I've never used it personally but it provides a wrapper to the 7za.dll COM library which may be helpful to you.

I've written utilities that utilise 7-Zip via a process myself and haven't had issues though I've never tried to do StdIn and StdOut stuff. In the Help files I have with my version of 7-Zip the page on the -si switch states:

Note: The current version of 7-Zip does not support reading of archives from stdin.

Note sure if this might be the source of your problem, with specifying both switches it might be confusing 7-Zip.

The examples they show in the help seem to show that -so is used to redirect the output to standard out but requires normal file based inputs to do so.

Upvotes: 4

Mark Rushakoff
Mark Rushakoff

Reputation: 258228

You might need to use 7za.exe, which is the "Commandline version" on the 7z download page. I see that you are currently using 7z.exe, and I'm pretty sure that's a problem I've encountered before as well.


Actually, I think I switched to PeaZip because of the troubles 7z was giving me. PeaZip is a wrapper around 7z and a few other compression utilities, and PeaZip has a little bit better command line interface.

Upvotes: 0

Related Questions