java.util.zip on hpux

C

ctippur

All,

I have used java.util.zip package to compress a folder recursively
(which contains sub folders) using the package java.util.zip. It
created a zip file after that. I am unable to unzip OR gunzip this file
on HPUX.
There are couple of problems on HPUX:
1. there is no unzip tool that comes with the OS distribution
2. gunzip tool that resides in /usr/contrib/bin folder is not
appropriate. When I rename the file to .gz file and run gunzip on it,
it comes with "file exists" error.
I want to achieve the following things:
a) I want to compress a folder recursively.
b) I want to be able to access the files within the compressed file via
java
c) the compressed file must be "uncompressable" via tools available
with OS. THe most convenient tool is compress as it is available
everywhere with regular OS distribution.

I have read on java docs that in order to achieve compress via gzip, we
can do that only to a file. We have to essentially tar the folder and
then gzip the tar ball. If we do it this way, can we access the files
within the tar ball via java?

On Solaris however, I was able to unzip it.
Solaris8 $ unzip XXXX.zip
Archive: XXXX.zip
inflating: bin/hpux-risc
inflating: bin/root.sh
inflating: bin/createpackage
inflating: bin/env
inflating: bin/postinstallcheck
inflating: bin/agentconnection.sh
Solaris8 $ ls -ltr
total 1168
-rw-r--r-- 1 itv1 itv1 585115 Dec 1 16:23 XXXX.zip
drwxr-xr-x 2 itv1 itv1 618 Dec 1 16:23 bin

I appreciate any inputs on this issue.
Thanks
- Shekar
 
R

Roedy Green

I have used java.util.zip package to compress a folder recursively
(which contains sub folders) using the package java.util.zip. It
created a zip file after that. I am unable to unzip OR gunzip this file
on HPUX.

See what you can do with Jar.exe.

See http://mindprod.com/jgloss/zip.html
for how to write some code to unpack a zip.

Here is a sample of the sort of thing you need:

/**
* Unpack one zip file, and put all its contents into the target
directory.
* It may contain some deadwood, but so long as we process zips in
the
* proper order the deadwood will be over written.
*
* @param zd
* Which zip file to unpack.
* @throws IOException
*/
public static void unpackOneZip ( MiniZD zd ) throws IOException
{

// can't use ZipInputStream, since getSize would fail
File zf = new File( zd.getZipFilename( ZD.ON_TARGET ) );
ZipFile zip = new ZipFile( zf );
// for each element in the zip
// can't use for:each, only works with Iterator not
Enumeration.
for ( Enumeration e = zip.entries(); e.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry)e.nextElement();
String elementName = entry.getName();
Replicator.doing( "unpacking: " + elementName );
// inside zip, uses / names.
File elementFile = new File(
ConfigForReceiver.RECEIVER_BASE_DIR,
elementName.replace( '/', File.separatorChar ) );
IO.ensureDirectoryExists( elementFile.getParent() );
// test for deleted marker, possibly null
if ( "deleted".equals( entry.getComment() ) )
{
elementFile.delete();
StatsForReceiver.deletedFilesCount++ ;
}
else
{
ft.copy( zip.getInputStream( entry ), elementFile );
elementFile.setLastModified( entry.getTime() );
StatsForReceiver.receivedFilesCount++ ;
}
} // end for each element in the zip

zip.close();
} // end unpackOneZip
 
C

ctippur

Thanks for the reply.
This certainly works with jar utility.
Is jar available on hpux and AIX with standard distribution?

- Shekar
 
T

Thomas Kellerer

All,

I have used java.util.zip package to compress a folder recursively
(which contains sub folders) using the package java.util.zip. It
created a zip file after that. I am unable to unzip OR gunzip this file
on HPUX.
There are couple of problems on HPUX:
1. there is no unzip tool that comes with the OS distribution
2. gunzip tool that resides in /usr/contrib/bin folder is not
appropriate. When I rename the file to .gz file and run gunzip on it,
it comes with "file exists" error.

I assume you used java.util.ZipOutputStream. Did you try to use
java.util.zip.GZIPOutputStream and then try gunzip on the created archive?

Thomas
 
C

ctippur

The problem with java.util.zip.GZIPOutputStream is that we can only
use this for a single file. I can certainly tar the folder and try to
use this to compress the tar bundle but I am not sure how I can access
the files within the gzip bindle via java.

- Shekar
 
C

ctippur

Is there a java utility available which uses the compress algorithm?
compress is a tool available on all the platforms (as far as I can
tell).

- Shekar
 
C

Chris Uppal

Is there a java utility available which uses the compress algorithm?
compress is a tool available on all the platforms (as far as I can
tell).

No.

The best bet for cross-platform compression/decompression (especially if you
want to compress more than one file into an archive) is ZIP format (which is
almost the same as JAR format).

A bit of background. There are many compression /algorithms/ and there are
many compression /programs/, but the two are not the same. Any given
compression program will use one or more specific algorithms to compress the
data, and then will write the data out into a specific file /format/. So to
read the file you have to use a program (not necessarily the same program) that
understands that format.

As far as I know, the only compression /algorithm/ that is in widespread use
(i.e. you can expect to find tools that use it on almost any default
installation of almost any OS) is the so-called gzip algorithm. That
algorithm is used in at least two compression /formats/, the format used by the
UNIX-based tool gzip, and the format used by the DOS/Windows/etc-based tool(s)
PKZIP (and other members of that family). Despite the similarity of their
names, those two tools use completely different file formats -- not least
because ZIP is a multiple-file archive format, whereas gzip is (in normal use)
a single-file format.

There are many, many, programs on Windows that understand the ZIP format.
Windows itself does in recent versions. You cannot assume that a Windows
installation will have any tools that understand the gzip format (or the
combination of gzip and tar which is often used). Many Windows installations
/do/ have such tools, but none come as standard, and no single third-party tool
is so dominant that you can assume that users will have installed it, or are
willing to do so.

All UNIX-style boxed understand the gzip format, and they all understand the
tar format too. There is a freely-distributable tool for handling ZIP files
called "zip" (http://www.info-zip.org/), that tool is installed on many
UNIX-like systems by default, but I don't know whether its included in /all/ of
them (I can't think of any reason why it should not be included).

Java has a (somewhat weak) implementation of ZIP file handling and it
understands the gzip format too, but it does not have standard libraries for
handling "tar" files, so it does not (by default) understand the common UNIX
"tarball" (tar + gzip) format.

(The "compress" program, and the corresponding format, is essentially defunct.
You will find it hard to find any tools for non-UNIX OSes that understand it,
and you certainly cannot assume that any particular Windows installation will
have one installed.)

So, you are limited to ZIP and gzip compression if you want to do it in Java.
Neither ZIP nor gzip is so universal that any given installation of any given
OS is /certain/ to have tools for handling it. Of the two ZIP comes closest,
and I would say that it's a rare Windows or UNIX-style installation that
doesn't understand ZIP.

-- chris
 
C

ctippur

Chris,

Thanks for the excellent explanation. We have 3 hpux boxes that do not
have zip utility but they do have man pages for the same. I am not sure
what to make out of that but again, thanks for putting everything in
perspective.

- Shekar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top