
br.com.objectos.io.Zip Maven / Gradle / Ivy
/*
* Copyright (C) 2011-2021 Objectos Software LTDA.
*
* 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.
*/
package br.com.objectos.io;
import static br.com.objectos.lang.Lang.checkNotNull;
import br.com.objectos.lang.Lang;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
final class Zip implements DirectoryContentsVisitor, ZipOptionVisitor {
private final byte[] buffer;
private final Closeable entryCloseable = new Closeable() {
@Override
public final void close() throws IOException {
zip.closeEntry();
}
};
private String prefix = "";
private boolean recursePaths;
private final Directory workingDirectory;
private final ZipOutputStream zip;
private final NotFound zipFile;
Zip(NotFound zipFile, Directory workingDirectory, byte[] buffer, ZipOutputStream zip) {
this.zipFile = zipFile;
this.workingDirectory = workingDirectory;
this.buffer = buffer;
this.zip = zip;
}
public static RegularFile zip(
Directory workingDirectory, NotFound zipFile,
ZipOption option,
String[] files) throws IOException {
checkNotNull(workingDirectory, "workingDirectory == null");
checkNotNull(zipFile, "zipFile == null");
checkNotNull(option, "option == null");
checkNotNull(files, "files == null");
byte[] buffer;
buffer = Io.createBuffer();
Zip zip;
zip = open(workingDirectory, zipFile, buffer);
option.acceptZipOptionVisitor(zip);
return zip.execute(files);
}
private static Zip open(
Directory workingDirectory, NotFound zipFile, byte[] buffer)
throws IOException {
OutputStream writeStream;
writeStream = zipFile.openOutputStream();
ZipOutputStream zip;
zip = new ZipOutputStream(writeStream);
return new Zip(zipFile, workingDirectory, buffer, zip);
}
public final RegularFile execute(String[] files) throws IOException {
IOException rethrow;
rethrow = null;
try {
execute0(files);
} catch (IOException e) {
rethrow = e;
} finally {
rethrow = Lang.close(rethrow, zip);
}
Lang.rethrowIfNecessary(rethrow);
FsObject result;
result = zipFile.refresh();
return result.toRegularFile();
}
@Override
public final void visitDirectory(Directory directory) throws IOException {
String entryName;
entryName = prefix + directory.getName() + '/';
ZipEntry entry;
entry = new ZipEntry(entryName);
zip.putNextEntry(entry);
zip.closeEntry();
if (!recursePaths) {
return;
}
String previousPrefix;
previousPrefix = prefix;
prefix = entryName;
directory.visitContents(this);
prefix = previousPrefix;
}
@Override
public final void visitRecursePaths() {
recursePaths = true;
}
@Override
public final void visitRegularFile(RegularFile file) throws IOException {
String entryName;
entryName = prefix + file.getName();
ZipEntry entry;
entry = new ZipEntry(entryName);
zip.putNextEntry(entry);
IOException rethrow;
rethrow = null;
InputStream in;
in = file.openInputStream();
try {
Io.copy(in, zip, buffer);
} catch (IOException e) {
rethrow = e;
} finally {
rethrow = Lang.close(rethrow, in);
rethrow = Lang.close(rethrow, entryCloseable);
}
Lang.rethrowIfNecessary(rethrow);
}
private void execute0(String[] files) throws IOException {
for (int i = 0, length = files.length; i < length; i++) {
String fileName;
fileName = files[i];
if (fileName.equals(".")) {
workingDirectory.visitContents(this);
continue;
}
FsObject object;
try {
object = workingDirectory.resolve(fileName);
} catch (NullPointerException npe) {
throw new NullPointerException("files[" + i + "] == null");
}
if (object.isDirectory()) {
Directory directory;
directory = object.toDirectory();
visitDirectory(directory);
}
else if (object.isRegularFile()) {
RegularFile file;
file = object.toRegularFile();
visitRegularFile(file);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy