3

Is there a way to uncompress .Z files using php?

Mark
  • 2,380
  • 11
  • 29
  • 49
jon
  • 1,429
  • 1
  • 23
  • 40
  • Depends on what compression algorithm is used, where does the file come from? If it's just a Zip file with another extension you should be able to unzip it with http://php.net/manual/en/book.zip.php – ChrisR Apr 16 '12 at 13:18
  • Thanks Chris R, I have tried that, but it didn't work... (it did for .zip files)... Some info on the file i am trying to uncompress: The Z extension is a Unix zip file, a pre-cursor to gz and tar, but still in common use in Unix environments – any ideas if this is possible to uncompress with php? – jon Apr 16 '12 at 13:28

2 Answers2

1

After searching some i've found that .z files are files that were compressed using the compress program. If your php installation allows shell_exec and your webserver is running unix/linux you could run the uncompress program on your server. This is the (untested) idea:

<?php
$file = '/tmp/archive.z';
shell_exec("uncompress $file");
ChrisR
  • 14,370
  • 16
  • 70
  • 107
1

Nowadays uncompress is just nothing more than a one-liner invoking gzip with proper options. To use gzip, you don't have execute shell. You can use Zlib extension instead. I'd try something like:

<?php
$contents = zlib_decode(file_get_contents('/path/file.Z'));
vartec
  • 131,205
  • 36
  • 218
  • 244
  • Thanks Vartec for your response, I don't appear to be able to access the Zlib extention on my version of php... is there any other work around other than ChrisR suggestion? – jon Apr 16 '12 at 16:07
  • 1
    @jon: if you cannot access zlib, there isn't. FYI, `uncompress ` is equivalent to `gzip -d`. – vartec Apr 16 '12 at 16:09
  • I didn't complete understand your response about uncompress, however between you, ChrisR and google I have found a solution that works on my server: shell_exec("gunzip $file"); thanks for your help. Regards J – jon Apr 17 '12 at 08:43
  • @jon: I've meant, that `shell_exec("uncompres $file")` is same as `shell_exec("gzip -d $file")`, which as you well noted is same as `shell_exec("gunzip $file")` – vartec Apr 17 '12 at 08:47
  • 1
    The anonymous edit, "So that beginner users can see whether or not their code was successful, it's important to show them to use `var_dump()` and not `echo($contents)`--it's generally important to show users how to check for success; so, I hope this change can stay applied." should have been a comment instead. – Dan Is Fiddling By Firelight Dec 02 '13 at 04:29