Re: JarFile/ZipFile from byte array without temp file
On Wed, 27 Jun 2007 08:29:29 -0700, Karsten Wutzke <kwutzke@web.de>
wrote, quoted or indirectly quoted someone who said :
Subject says it all... how do I create a JarFile/ZipFile instance from
a byte array without outputting the byte[] to a temporary file and
reading it back via the JarFile/ZipFile constructors??
Here is a piece of code from The Replicator. Full code is posted at
http://mindprod.com/products1.html#REPLICATOR
There is a lot of irrelevancy for you, but the key is zip.write.
/**
* Add one element to a newly created zip file.
*
* @param zip ZipOutputStream to tack this element on the end.
* @param zd descriptor of the zip file
* @param fd descriptor of file to be added.
*
* @throws IOException
*/
public static void addOneElement( ZipOutputStream zip,
MaxiZD zd,
MaxiFD fd ) throws IOException
{
String elementName = fd.getFilename( MaxiFD.INSIDE_ZIP );
System.out.println( "packing " + elementName );
ZipEntry entry = new ZipEntry( elementName );
entry.setTime( fd.getTimestamp() );
int oldFileLength = (int) fd.getFileLength();
int fileLength;
int shrinkage;
File elementFile = new File( fd.getFilename( MaxiFD.ON_SOURCE
) );
// strings all interned so == compare is safe,
// but use equals for safety. Interning speeds things up.
String extension = fd.getExtension();
boolean isHtml =
extension.equals( "html" ) || extension.equals( "htm"
);
if ( isHtml && ConfigForSender.COMPACT_HTML.equals( "original"
) )
{
if ( !elementFile.canWrite() )
{
ReplicatorSender
.fatal( "Read-only original files cannot be
compacted: "
+ fd.getFilename( MaxiFD.ON_SOURCE )
);
}
// compact the original
compactor.compactFile( elementFile, true/* quiet */ );
// no need to refresh elementFile even though it is now a
different
// directory entry.
fileLength = (int) elementFile.length();
shrinkage = oldFileLength - fileLength;
if ( shrinkage != 0 )
{
// set file back to its original date so it will be in
the
// proper zip.
elementFile.setLastModified( fd.getTimestamp() );
if ( ConfigForSender.DEBUGGING )
{
System.out
.println( "original file compacted from "
+ oldFileLength
+ " to "
+ fileLength
+ " bytes." );
}
// record the new size so we will recognize it.
zd.livewood -= shrinkage;
fd.setFileLength( fileLength );
}
}
else
{
// did not compact the original. It should not have
changed.
fileLength = (int) elementFile.length();
if ( oldFileLength != fileLength )
{
System.err.println( fd );
ReplicatorSender
.fatal( "file "
+ elementName
+ " has unexpectedly changed length
from "
+ oldFileLength
+ " to "
+ fileLength
+ " bytes."
+ "\n"
+ "Please don't update distribution
files while the Replicator is running." );
}
}
// no need to setCRC, setSize, computed automatically.
zip.putNextEntry( entry );
if ( isHtml && ConfigForSender.COMPACT_HTML.equals(
"distributed" ) )
{
// compact just the distribution, leaving original intact.
// read entire file, compact and append as element.
String wholeFile = HunkIO.readEntireFile( elementFile );
String compacted = compactor.compactString( wholeFile );
byte[] octets = compacted.getBytes( /* default encoding */
);
fileLength = octets.length;
shrinkage = oldFileLength - fileLength;
if ( shrinkage != 0 )
{
if ( ConfigForSender.DEBUGGING )
{
System.out
.println( "distributed file compacted from
"
+ oldFileLength
+ " to "
+ fileLength
+ " bytes." );
}
// We don't adjust livewood or fd.
// We need to recognise the file in future in its
fluffy form
// with its fluffy length. Its old date is just fine.
}
zip.write( octets );
}
else
{
// no compacting
ft.copy( elementFile, zip, false/* don't close target */
);
}
zip.closeEntry();
}
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com