use of org.apache.tools.zip.ZipExtraField in project ant by apache.
the class Zip method zipFile.
/**
* Adds a new entry to the archive, takes care of duplicates as well.
*
* @param in the stream to read data for the entry from. The
* caller of the method is responsible for closing the stream.
* @param zOut the stream to write to.
* @param vPath the name this entry shall have in the archive.
* @param lastModified last modification time for the entry.
* @param fromArchive the original archive we are copying this
* entry from, will be null if we are not copying from an archive.
* @param mode the Unix permissions to set.
*
* @since Ant 1.5.2
* @throws IOException on error
*/
protected void zipFile(final InputStream in, final ZipOutputStream zOut, final String vPath, final long lastModified, final File fromArchive, final int mode) throws IOException {
if (entries.containsKey(vPath)) {
if ("preserve".equals(duplicate)) {
logWhenWriting(vPath + " already added, skipping", Project.MSG_INFO);
return;
}
if ("fail".equals(duplicate)) {
throw new BuildException("Duplicate file %s was found and the duplicate attribute is 'fail'.", vPath);
}
// duplicate equal to add, so we continue
logWhenWriting("duplicate file " + vPath + " found, adding.", Project.MSG_VERBOSE);
} else {
logWhenWriting("adding entry " + vPath, Project.MSG_VERBOSE);
}
entries.put(vPath, vPath);
if (!skipWriting) {
final ZipEntry ze = new ZipEntry(vPath);
ze.setTime(fixedModTime != null ? modTimeMillis : lastModified);
ze.setMethod(doCompress ? ZipEntry.DEFLATED : ZipEntry.STORED);
// if the input stream doesn't support mark/reset ability, we wrap it in a
// stream that adds that support.
// Note: We do *not* close this newly created wrapping input stream, since
// we don't "own" the underlying input stream that's passed to us and closing
// that is the responsibility of the caller.
final InputStream markableInputStream = in.markSupported() ? in : new BufferedInputStream(in);
/*
* ZipOutputStream.putNextEntry expects the ZipEntry to
* know its size and the CRC sum before you start writing
* the data when using STORED mode - unless it is seekable.
*
* This forces us to process the data twice.
*/
if (!zOut.isSeekable() && !doCompress) {
long size = 0;
final CRC32 cal = new CRC32();
markableInputStream.mark(Integer.MAX_VALUE);
final byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
do {
size += count;
cal.update(buffer, 0, count);
count = markableInputStream.read(buffer, 0, buffer.length);
} while (count != -1);
markableInputStream.reset();
ze.setSize(size);
ze.setCrc(cal.getValue());
}
ze.setUnixMode(mode);
final ZipExtraField[] extra = getCurrentExtraFields();
if (extra != null) {
ze.setExtraFields(extra);
}
zOut.putNextEntry(ze);
final byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
do {
if (count != 0) {
zOut.write(buffer, 0, count);
}
count = markableInputStream.read(buffer, 0, buffer.length);
} while (count != -1);
}
addedFiles.add(vPath);
}
use of org.apache.tools.zip.ZipExtraField in project ant by apache.
the class ZipExtraFieldTest method testExtraField.
private void testExtraField(Zip testInstance, boolean expectZip64) throws IOException {
File f = File.createTempFile("ziptest", ".zip");
f.delete();
ZipFile zf = null;
try {
testInstance.setDestFile(f);
final ZipResource r = new ZipResource() {
public String getName() {
return "x";
}
public boolean isExists() {
return true;
}
public boolean isDirectory() {
return false;
}
public long getLastModified() {
return 1;
}
public InputStream getInputStream() {
return new ByteArrayInputStream(new byte[0]);
}
public ZipExtraField[] getExtraFields() {
return new ZipExtraField[] { new JarMarker() };
}
};
testInstance.add(new ResourceCollection() {
public boolean isFilesystemOnly() {
return false;
}
public int size() {
return 1;
}
public Iterator<Resource> iterator() {
return Collections.<Resource>singleton(r).iterator();
}
});
testInstance.execute();
zf = new ZipFile(f);
ZipEntry ze = zf.getEntry("x");
assertNotNull(ze);
assertEquals(expectZip64 ? 2 : 1, ze.getExtraFields().length);
assertTrue(ze.getExtraFields()[0] instanceof JarMarker);
if (expectZip64) {
assertTrue(ze.getExtraFields()[1] instanceof Zip64ExtendedInformationExtraField);
}
} finally {
ZipFile.closeQuietly(zf);
if (f.exists()) {
f.delete();
}
}
}
Aggregations