
com.centurylink.mdw.cli.Zip Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mdw-common Show documentation
Show all versions of mdw-common Show documentation
MDW is a microservices based workflow framework
/*
* Copyright (C) 2018 CenturyLink, Inc.
*
* 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 com.centurylink.mdw.cli;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zip implements Operation {
private File srcDir;
private File zipFile;
public Zip(File srcDir, File zipFile) {
this.srcDir = srcDir;
this.zipFile = zipFile;
}
@Override
public Operation run(ProgressMonitor... progressMonitors) throws IOException {
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
Path dirPath = srcDir.toPath();
Files.walkFileTree(dirPath, new SimpleFileVisitor() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(dirPath.relativize(file).toString().replace('\\', '/')));
Files.copy(file, zos);
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (!dir.equals(dirPath)) {
zos.putNextEntry(new ZipEntry(dirPath.relativize(dir).toString().replace('\\', '/') + "/"));
zos.closeEntry();
}
return FileVisitResult.CONTINUE;
}
});
}
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy