2015-03-31 19:10:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Ken Ellinwood
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-04-19 16:18:24 +02:00
|
|
|
|
2015-03-31 19:10:03 +02:00
|
|
|
package kellinwood.zipio;
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
import kellinwood.logging.LoggerInterface;
|
|
|
|
import kellinwood.logging.LoggerManager;
|
|
|
|
|
2015-03-31 19:10:03 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.HashSet;
|
2018-04-19 16:18:24 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2015-03-31 19:10:03 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-04-19 16:18:24 +02:00
|
|
|
public class ZipOutput {
|
2015-03-31 19:10:03 +02:00
|
|
|
|
|
|
|
static LoggerInterface log;
|
|
|
|
|
|
|
|
String outputFilename;
|
|
|
|
OutputStream out = null;
|
|
|
|
int filePointer = 0;
|
|
|
|
|
|
|
|
List<ZioEntry> entriesWritten = new LinkedList<ZioEntry>();
|
|
|
|
Set<String> namesWritten = new HashSet<String>();
|
2018-04-19 16:18:24 +02:00
|
|
|
|
|
|
|
public ZipOutput(String filename) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
this.outputFilename = filename;
|
2018-04-19 16:18:24 +02:00
|
|
|
File ofile = new File(outputFilename);
|
2015-03-31 19:10:03 +02:00
|
|
|
init(ofile);
|
|
|
|
}
|
2018-04-19 16:18:24 +02:00
|
|
|
|
|
|
|
public ZipOutput(File outputFile) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
this.outputFilename = outputFile.getAbsolutePath();
|
|
|
|
File ofile = outputFile;
|
|
|
|
init(ofile);
|
|
|
|
}
|
2018-04-19 16:18:24 +02:00
|
|
|
|
|
|
|
private void init(File ofile) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
if (ofile.exists()) ofile.delete();
|
2018-04-19 16:18:24 +02:00
|
|
|
out = new FileOutputStream(ofile);
|
|
|
|
if (getLogger().isDebugEnabled()) ZipListingHelper.listHeader(getLogger());
|
|
|
|
|
2015-03-31 19:10:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public ZipOutput(OutputStream os) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
out = os;
|
|
|
|
}
|
2018-04-19 16:18:24 +02:00
|
|
|
|
2015-03-31 19:10:03 +02:00
|
|
|
private static LoggerInterface getLogger() {
|
|
|
|
if (log == null) log = LoggerManager.getLogger(ZipOutput.class.getName());
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public void write(ZioEntry entry) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
String entryName = entry.getName();
|
2018-04-19 16:18:24 +02:00
|
|
|
if (namesWritten.contains(entryName)) {
|
2015-03-31 19:10:03 +02:00
|
|
|
getLogger().warning("Skipping duplicate file in output: " + entryName);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-19 16:18:24 +02:00
|
|
|
entry.writeLocalEntry(this);
|
|
|
|
entriesWritten.add(entry);
|
|
|
|
namesWritten.add(entryName);
|
|
|
|
if (getLogger().isDebugEnabled()) ZipListingHelper.listEntry(getLogger(), entry);
|
|
|
|
|
2015-03-31 19:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public void close() throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
CentralEnd centralEnd = new CentralEnd();
|
2018-04-19 16:18:24 +02:00
|
|
|
|
|
|
|
centralEnd.centralStartOffset = (int) getFilePointer();
|
|
|
|
centralEnd.numCentralEntries = centralEnd.totalCentralEntries = (short) entriesWritten.size();
|
|
|
|
|
2015-03-31 19:10:03 +02:00
|
|
|
for (ZioEntry entry : entriesWritten) {
|
2018-04-19 16:18:24 +02:00
|
|
|
entry.write(this);
|
2015-03-31 19:10:03 +02:00
|
|
|
}
|
2018-04-19 16:18:24 +02:00
|
|
|
|
|
|
|
centralEnd.centralDirectorySize = (int) (getFilePointer() - centralEnd.centralStartOffset);
|
2015-03-31 19:10:03 +02:00
|
|
|
centralEnd.fileComment = "";
|
2018-04-19 16:18:24 +02:00
|
|
|
|
|
|
|
centralEnd.write(this);
|
|
|
|
|
|
|
|
if (out != null) try {
|
|
|
|
out.close();
|
|
|
|
} catch (Throwable t) {
|
|
|
|
}
|
2015-03-31 19:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getFilePointer() throws IOException {
|
|
|
|
return filePointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public void writeInt(int value) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
byte[] data = new byte[4];
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2018-04-19 16:18:24 +02:00
|
|
|
data[i] = (byte) (value & 0xFF);
|
2015-03-31 19:10:03 +02:00
|
|
|
value = value >> 8;
|
|
|
|
}
|
2018-04-19 16:18:24 +02:00
|
|
|
out.write(data);
|
2015-03-31 19:10:03 +02:00
|
|
|
filePointer += 4;
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public void writeShort(short value) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
byte[] data = new byte[2];
|
|
|
|
for (int i = 0; i < 2; i++) {
|
2018-04-19 16:18:24 +02:00
|
|
|
data[i] = (byte) (value & 0xFF);
|
|
|
|
value = (short) (value >> 8);
|
2015-03-31 19:10:03 +02:00
|
|
|
}
|
2018-04-19 16:18:24 +02:00
|
|
|
out.write(data);
|
2015-03-31 19:10:03 +02:00
|
|
|
filePointer += 2;
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public void writeString(String value) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
|
|
|
|
byte[] data = value.getBytes();
|
2018-04-19 16:18:24 +02:00
|
|
|
out.write(data);
|
2015-03-31 19:10:03 +02:00
|
|
|
filePointer += data.length;
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public void writeBytes(byte[] value) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
out.write(value);
|
2015-03-31 19:10:03 +02:00
|
|
|
filePointer += value.length;
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
public void writeBytes(byte[] value, int offset, int length) throws IOException {
|
2015-03-31 19:10:03 +02:00
|
|
|
|
2018-04-19 16:18:24 +02:00
|
|
|
out.write(value, offset, length);
|
2015-03-31 19:10:03 +02:00
|
|
|
filePointer += length;
|
2018-04-19 16:18:24 +02:00
|
|
|
}
|
2015-03-31 19:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|