Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/OpenConcerto/src/org/openconcerto/utils/Zip.java |
---|
13,10 → 13,13 |
package org.openconcerto.utils; |
import org.openconcerto.utils.cc.ITransformerExn; |
import java.io.BufferedInputStream; |
import java.io.BufferedOutputStream; |
import java.io.ByteArrayInputStream; |
import java.io.ByteArrayOutputStream; |
import java.io.Closeable; |
import java.io.File; |
import java.io.FileInputStream; |
import java.io.FileNotFoundException; |
27,6 → 30,9 |
import java.nio.ByteBuffer; |
import java.util.Enumeration; |
import java.util.Set; |
import java.util.function.Function; |
import java.util.jar.JarEntry; |
import java.util.jar.JarOutputStream; |
import java.util.zip.CRC32; |
import java.util.zip.DeflaterOutputStream; |
import java.util.zip.InflaterInputStream; |
40,7 → 46,7 |
* @author ILM Informatique |
* @see org.openconcerto.utils.Unzip |
*/ |
public class Zip { |
public class Zip implements Closeable { |
static public byte[] deflate(final String s) throws IOException { |
return deflate(s.getBytes(StringUtils.UTF8)); |
121,10 → 127,16 |
createFrom(src, dest, entriesName).close(); |
} |
static public Zip createJar(final OutputStream out) { |
return new Zip(out, JarOutputStream::new, JarEntry::new); |
} |
// *** Instance |
private final OutputStream outstream; |
private final ITransformerExn<OutputStream, ZipOutputStream, IOException> createZipStream; |
private ZipOutputStream zos; |
private final Function<String, ZipEntry> createEntry; |
// is an entry open, ie addEntry() has been called but closeEntry() not yet |
private boolean entryOpen; |
144,27 → 156,40 |
* @param out un stream dans lequel écrire. |
*/ |
public Zip(OutputStream out) { |
this(out, ZipOutputStream::new, ZipEntry::new); |
} |
public Zip(OutputStream out, final ITransformerExn<OutputStream, ZipOutputStream, IOException> createZipStream, final Function<String, ZipEntry> createEntry) { |
this.outstream = out; |
this.createZipStream = createZipStream; |
this.zos = null; |
this.createEntry = createEntry; |
this.entryOpen = false; |
} |
@Override |
public synchronized void close() throws IOException { |
if (this.zos != null) { |
// ferme aussi le FileOutputStream |
this.zos.close(); |
} else { |
this.outstream.close(); |
} |
} |
// *** Ecriture |
private synchronized ZipOutputStream getOutStream() { |
private synchronized ZipOutputStream getOutStream() throws IOException { |
if (this.zos == null) { |
this.zos = new ZipOutputStream(this.outstream); |
this.zos = this.createZipStream.transformChecked(this.outstream); |
} |
return this.zos; |
} |
public ZipEntry createEntry(String name) { |
return this.createEntry.apply(name); |
} |
/** |
* Ajoute newFile dans ce fichier. Il sera enregistré dans le zip directement à la racine. |
* |
173,8 → 198,15 |
*/ |
public void zip(File newFile) throws IOException { |
// on ne garde que la derniere partie du chemin |
this.zip(newFile.getName(), newFile); |
} |
public void zip(final String entryName, final File newFile) throws IOException { |
final ZipEntry entry = this.createEntry(entryName); |
// TODO call ZipEntry.setCreationTime() |
entry.setTime(newFile.lastModified()); |
try (final BufferedInputStream ins = new BufferedInputStream(new FileInputStream(newFile))) { |
this.zip(newFile.getName(), ins); |
this.zip(entry, ins); |
} |
} |
185,9 → 217,13 |
* @param in l'ajout. |
* @throws IOException si in ne peut etre zippé. |
*/ |
public synchronized void zip(String name, InputStream in) throws IOException { |
this.putNextEntry(name); |
public void zip(String name, InputStream in) throws IOException { |
this.zip(this.createEntry(name), in); |
} |
public synchronized void zip(final ZipEntry entry, InputStream in) throws IOException { |
this.putNextEntry(entry); |
byte b[] = new byte[512]; |
int len = 0; |
while ((len = in.read(b)) != -1) { |
215,7 → 251,7 |
* @throws IOException if an error occurs. |
*/ |
public synchronized void zipNonCompressed(String name, byte[] in) throws IOException { |
final ZipEntry entry = new ZipEntry(name); |
final ZipEntry entry = createEntry(name); |
entry.setMethod(ZipEntry.STORED); |
final CRC32 crc = new CRC32(); |
crc.update(in); |
235,7 → 271,7 |
* @return a stream to write to the entry. |
* @throws IOException if a pb occurs. |
*/ |
public synchronized OutputStream createEntry(String name) throws IOException { |
public synchronized OutputStream createEntryStream(String name) throws IOException { |
this.putNextEntry(name); |
return new BufferedOutputStream(this.getOutStream()) { |
public void close() throws IOException { |
246,7 → 282,7 |
} |
private final synchronized void putNextEntry(String name) throws IOException, FileNotFoundException { |
this.putNextEntry(new ZipEntry(name)); |
this.putNextEntry(createEntry(name)); |
} |
private final synchronized void putNextEntry(ZipEntry entry) throws IOException, FileNotFoundException { |