use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project stanbol by apache.
the class ConfigUtils method copyDefaultConfig.
/**
* Initialises the default configuration for the SolrYard based on data in the parsed bundle. The
* configuration will be copied to the parsed root directory.
*
* @param clazzInArchive
* This class is used to identify the archive containing the default configuration. Parsing
* <code>null</code> causes this class to be used and therefore initialises the default
* configuration contained by the SolrYard bundle.
* @param rootDir
* the target directory for the configuration.
* @param override
* if true existing configurations are overridden.
* @return the root directory of the solr configuration (same as parsed as rootDir)
* @throws IOException
* On any IO error while coping the configuration
* @throws IllegalStateException
* If the parsed rootDir does exist but is not a directory.
* @throws IllegalArgumentException
* iIf <code>null</code> is parsed as rootDir or if the parsed bundle does not contain the
* required information to set up an configuration
*/
public static File copyDefaultConfig(Class<?> clazzInArchive, File rootDir, boolean override) throws IOException, IllegalStateException, IllegalArgumentException {
if (rootDir == null) {
throw new IllegalArgumentException("The parsed root directory MUST NOT be NULL!");
}
if (rootDir.exists() && !rootDir.isDirectory()) {
throw new IllegalStateException("The parsed root directory " + rootDir.getAbsolutePath() + " extists but is not a directory!");
}
File sourceRoot = getSource(clazzInArchive != null ? clazzInArchive : ConfigUtils.class);
log.info("Init Solr Managed Directory form {} to {} (override={})", new Object[] { sourceRoot, rootDir, override });
if (sourceRoot.isFile()) {
ZipFile archive = new ZipFile(sourceRoot);
log.info(" - read from jar-file");
try {
for (@SuppressWarnings("unchecked") Enumeration<ZipArchiveEntry> entries = archive.getEntries(); entries.hasMoreElements(); ) {
ZipArchiveEntry entry = entries.nextElement();
if (!entry.isDirectory() && entry.getName().startsWith(CONFIG_DIR)) {
copyResource(rootDir, archive, entry, CONFIG_DIR, override);
}
}
} finally {
// regardless what happens we need to close the archive!
ZipFile.closeQuietly(archive);
}
} else {
// load from file
log.info(" - read from directory");
File source = new File(sourceRoot, CONFIG_DIR);
if (source.exists() && source.isDirectory()) {
FileUtils.copyDirectory(source, rootDir);
} else {
throw new FileNotFoundException("The SolrIndex default config was not found in directory " + source.getAbsolutePath());
}
}
return rootDir;
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tomee by apache.
the class JarPatcher method jar.
private void jar(final int method, final JarArchiveOutputStream jar, final File f, final String prefix) throws IOException {
final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
final ZipArchiveEntry zip = new ZipArchiveEntry(f, path);
zip.setMethod(method);
final JarArchiveEntry archiveEntry = new JarArchiveEntry(zip);
jar.putArchiveEntry(archiveEntry);
if (f.isDirectory()) {
jar.closeArchiveEntry();
final File[] files = f.listFiles();
if (files != null) {
for (final File child : files) {
jar(method, jar, child.getCanonicalFile(), prefix);
}
}
} else {
final InputStream is = new FileInputStream(f);
IOUtils.copy(is, jar);
is.close();
jar.closeArchiveEntry();
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project gitblit by gitblit.
the class CompressionUtils method zip.
/**
* Zips the contents of the tree at the (optionally) specified revision and
* the (optionally) specified basepath to the supplied outputstream.
*
* @param repository
* @param basePath
* if unspecified, entire repository is assumed.
* @param objectId
* if unspecified, HEAD is assumed.
* @param os
* @return true if repository was successfully zipped to supplied output
* stream
*/
public static boolean zip(Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) {
RevCommit commit = JGitUtils.getCommit(repository, objectId);
if (commit == null) {
return false;
}
boolean success = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
zos.setComment("Generated by Gitblit");
if (!StringUtils.isEmpty(basePath)) {
PathFilter f = PathFilter.create(basePath);
tw.setFilter(f);
}
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
ObjectReader reader = tw.getObjectReader();
long modified = commit.getAuthorIdent().getWhen().getTime();
while (tw.next()) {
FileMode mode = tw.getFileMode(0);
if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
continue;
}
tw.getObjectId(id, 0);
ObjectLoader loader = repository.open(id);
ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString());
FilestoreModel filestoreItem = null;
if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) {
filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id));
}
final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize();
entry.setSize(size);
entry.setComment(commit.getName());
entry.setUnixMode(mode.getBits());
entry.setTime(modified);
zos.putArchiveEntry(entry);
if (filestoreItem == null) {
//Copy repository stored file
loader.copyTo(zos);
} else {
//Copy filestore file
try (FileInputStream streamIn = new FileInputStream(filestoreManager.getStoragePath(filestoreItem.oid))) {
IOUtils.copyLarge(streamIn, zos);
} catch (Throwable e) {
LOGGER.error(MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e);
//Handle as per other errors
throw e;
}
}
zos.closeArchiveEntry();
}
zos.finish();
success = true;
} catch (IOException e) {
error(e, repository, "{0} failed to zip files from commit {1}", commit.getName());
} finally {
tw.close();
rw.dispose();
}
return success;
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project gitblit by gitblit.
the class PtServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("application/octet-stream");
response.setDateHeader("Last-Modified", lastModified);
response.setHeader("Cache-Control", "none");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
boolean windows = false;
try {
String useragent = request.getHeader("user-agent").toString();
windows = useragent.toLowerCase().contains("windows");
} catch (Exception e) {
}
byte[] pyBytes;
File file = runtimeManager.getFileOrFolder("tickets.pt", "${baseFolder}/pt.py");
if (file.exists()) {
// custom script
pyBytes = readAll(new FileInputStream(file));
} else {
// default script
pyBytes = readAll(getClass().getResourceAsStream("/pt.py"));
}
if (windows) {
// windows: download zip file with pt.py and pt.cmd
response.setHeader("Content-Disposition", "attachment; filename=\"pt.zip\"");
OutputStream os = response.getOutputStream();
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
// add the Python script
ZipArchiveEntry pyEntry = new ZipArchiveEntry("pt.py");
pyEntry.setSize(pyBytes.length);
pyEntry.setUnixMode(FileMode.EXECUTABLE_FILE.getBits());
pyEntry.setTime(lastModified);
zos.putArchiveEntry(pyEntry);
zos.write(pyBytes);
zos.closeArchiveEntry();
// add a Python launch cmd file
byte[] cmdBytes = readAll(getClass().getResourceAsStream("/pt.cmd"));
ZipArchiveEntry cmdEntry = new ZipArchiveEntry("pt.cmd");
cmdEntry.setSize(cmdBytes.length);
cmdEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
cmdEntry.setTime(lastModified);
zos.putArchiveEntry(cmdEntry);
zos.write(cmdBytes);
zos.closeArchiveEntry();
// add a brief readme
byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
ZipArchiveEntry txtEntry = new ZipArchiveEntry("readme.txt");
txtEntry.setSize(txtBytes.length);
txtEntry.setUnixMode(FileMode.REGULAR_FILE.getBits());
txtEntry.setTime(lastModified);
zos.putArchiveEntry(txtEntry);
zos.write(txtBytes);
zos.closeArchiveEntry();
// cleanup
zos.finish();
zos.close();
os.flush();
} else {
// unix: download a tar.gz file with pt.py set with execute permissions
response.setHeader("Content-Disposition", "attachment; filename=\"pt.tar.gz\"");
OutputStream os = response.getOutputStream();
CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, os);
TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
tos.setAddPaxHeadersForNonAsciiNames(true);
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
// add the Python script
TarArchiveEntry pyEntry = new TarArchiveEntry("pt");
pyEntry.setMode(FileMode.EXECUTABLE_FILE.getBits());
pyEntry.setModTime(lastModified);
pyEntry.setSize(pyBytes.length);
tos.putArchiveEntry(pyEntry);
tos.write(pyBytes);
tos.closeArchiveEntry();
// add a brief readme
byte[] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
TarArchiveEntry txtEntry = new TarArchiveEntry("README");
txtEntry.setMode(FileMode.REGULAR_FILE.getBits());
txtEntry.setModTime(lastModified);
txtEntry.setSize(txtBytes.length);
tos.putArchiveEntry(txtEntry);
tos.write(txtBytes);
tos.closeArchiveEntry();
// cleanup
tos.finish();
tos.close();
cos.close();
os.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.
the class Unzip method extractZipFile.
/**
* Unzips a file to a destination and returns the paths of the written files.
*/
public static ImmutableList<Path> extractZipFile(Path zipFile, ProjectFilesystem filesystem, Path relativePath, ExistingFileMode existingFileMode) throws IOException {
// if requested, clean before extracting
if (existingFileMode == ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES) {
try (ZipFile zip = new ZipFile(zipFile.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
filesystem.deleteRecursivelyIfExists(relativePath.resolve(entry.getName()));
}
}
}
ImmutableList.Builder<Path> filesWritten = ImmutableList.builder();
try (ZipFile zip = new ZipFile(zipFile.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String fileName = entry.getName();
Path target = relativePath.resolve(fileName);
if (entry.isDirectory()) {
// Create the directory and all its parent directories
filesystem.mkdirs(target);
} else {
// Create parent folder
filesystem.createParentDirs(target);
filesWritten.add(target);
// Write file
try (InputStream is = zip.getInputStream(entry)) {
if (entry.isUnixSymlink()) {
filesystem.createSymLink(target, filesystem.getPath(new String(ByteStreams.toByteArray(is), Charsets.UTF_8)), /* force */
true);
} else {
try (OutputStream out = filesystem.newFileOutputStream(target)) {
ByteStreams.copy(is, out);
}
}
}
// restore mtime for the file
filesystem.resolve(target).toFile().setLastModified(entry.getTime());
// TODO(shs96c): Implement what the comment below says we should do.
//
// Sets the file permissions of the output file given the information in {@code entry}'s
// extra data field. According to the docs at
// http://www.opensource.apple.com/source/zip/zip-6/unzip/unzip/proginfo/extra.fld there
// are two extensions that might support file permissions: Acorn and ASi UNIX. We shall
// assume that inputs are not from an Acorn SparkFS. The relevant section from the docs:
//
// <pre>
// The following is the layout of the ASi extra block for Unix. The
// local-header and central-header versions are identical.
// (Last Revision 19960916)
//
// Value Size Description
// ----- ---- -----------
// (Unix3) 0x756e Short tag for this extra block type ("nu")
// TSize Short total data size for this block
// CRC Long CRC-32 of the remaining data
// Mode Short file permissions
// SizDev Long symlink'd size OR major/minor dev num
// UID Short user ID
// GID Short group ID
// (var.) variable symbolic link filename
//
// Mode is the standard Unix st_mode field from struct stat, containing
// user/group/other permissions, setuid/setgid and symlink info, etc.
// </pre>
//
// From the stat man page, we see that the following mask values are defined for the file
// permissions component of the st_mode field:
//
// <pre>
// S_ISUID 0004000 set-user-ID bit
// S_ISGID 0002000 set-group-ID bit (see below)
// S_ISVTX 0001000 sticky bit (see below)
//
// S_IRWXU 00700 mask for file owner permissions
//
// S_IRUSR 00400 owner has read permission
// S_IWUSR 00200 owner has write permission
// S_IXUSR 00100 owner has execute permission
//
// S_IRWXG 00070 mask for group permissions
// S_IRGRP 00040 group has read permission
// S_IWGRP 00020 group has write permission
// S_IXGRP 00010 group has execute permission
//
// S_IRWXO 00007 mask for permissions for others
// (not in group)
// S_IROTH 00004 others have read permission
// S_IWOTH 00002 others have write permission
// S_IXOTH 00001 others have execute permission
// </pre>
//
// For the sake of our own sanity, we're going to assume that no-one is using symlinks,
// but we'll check and throw if they are.
//
// Before we do anything, we should check the header ID. Pfft!
//
// Having jumped through all these hoops, it turns out that InfoZIP's "unzip" store the
// values in the external file attributes of a zip entry (found in the zip's central
// directory) assuming that the OS creating the zip was one of an enormous list that
// includes UNIX but not Windows, it first searches for the extra fields, and if not found
// falls through to a code path that supports MS-DOS and which stores the UNIX file
// attributes in the upper 16 bits of the external attributes field.
//
// We'll support neither approach fully, but we encode whether this file was executable
// via storing 0100 in the fields that are typically used by zip implementations to store
// POSIX permissions. If we find it was executable, use the platform independent java
// interface to make this unpacked file executable.
Set<PosixFilePermission> permissions = MorePosixFilePermissions.fromMode(entry.getExternalAttributes() >> 16);
if (permissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
MoreFiles.makeExecutable(filesystem.resolve(target));
}
}
}
}
return filesWritten.build();
}
Aggregations