use of org.apache.commons.compress.archivers.zip.ZipFile in project buck by facebook.
the class ProjectFilesystemTest method testCreateZipPreservesExecutablePermissions.
@Test
public void testCreateZipPreservesExecutablePermissions() throws IOException {
// Create a empty executable file.
Path exe = tmp.newFile("test.exe");
MoreFiles.makeExecutable(exe);
// Archive it into a zipfile using `ProjectFileSystem.createZip`.
Path zipFile = tmp.getRoot().resolve("test.zip");
filesystem.createZip(ImmutableList.of(exe), zipFile);
// permissions.
try (ZipFile zip = new ZipFile(zipFile.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
assertTrue(entries.hasMoreElements());
ZipArchiveEntry entry = entries.nextElement();
Set<PosixFilePermission> permissions = MorePosixFilePermissions.fromMode(entry.getExternalAttributes() >> 16);
assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE));
assertFalse(entries.hasMoreElements());
}
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method isZipFile.
/**
* <p>
* isZipFile.
* </p>
*
* @param zipFile a {@link java.io.File} object.
* @return a boolean.
*/
public static boolean isZipFile(File zipFile) {
try {
ZipFile zf = new ZipFile(zipFile);
boolean isZip = zf.getEntries().hasMoreElements();
zf.close();
return isZip;
} catch (IOException e) {
return false;
}
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method isFolderExist.
/**
* 判断在指定的zip目录下,指定的文件夹是否存在
*
* @param zipFile
* @param pathName
* @return
*/
public static boolean isFolderExist(File zipFile, String pathName) {
ZipFile file = null;
try {
file = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> en = file.getEntries();
while (en.hasMoreElements()) {
ZipArchiveEntry entry = en.nextElement();
String name = entry.getName();
if (name.startsWith(pathName)) {
return true;
}
}
return false;
} catch (IOException e) {
} finally {
if (null != file) {
try {
file.close();
} catch (IOException e) {
}
}
}
return false;
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method extractZipFileToFolder.
/**
* 压zip文件中的某个文件到指定地方
*
* @param zipFile
* @param path
* @param destFolder
* @throws java.io.IOException
*/
public static File extractZipFileToFolder(File zipFile, String path, File destFolder) {
ZipFile zip;
File destFile = null;
try {
zip = new ZipFile(zipFile);
ZipArchiveEntry zipArchiveEntry = zip.getEntry(path);
if (null != zipArchiveEntry) {
String name = zipArchiveEntry.getName();
name = FilenameUtils.getName(name);
destFile = new File(destFolder, name);
FileMkUtils.mkdirs(destFolder);
destFile.createNewFile();
InputStream is = zip.getInputStream(zipArchiveEntry);
FileOutputStream fos = new FileOutputStream(destFile);
int length = 0;
byte[] b = new byte[1024];
while ((length = is.read(b, 0, 1024)) != -1) {
fos.write(b, 0, length);
}
is.close();
fos.close();
}
if (null != zip)
ZipFile.closeQuietly(zip);
} catch (IOException e) {
}
return destFile;
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method unzip.
/**
* <p>
* unzip.
* </p>
*
* @param zipFile a {@link java.io.File} object.
* @param destination a {@link String} object.
* @param encoding a {@link String} object.
* @return a {@link java.util.List} object.
*/
public static List<String> unzip(final File zipFile, final String destination, String encoding) {
List<String> fileNames = new ArrayList<String>();
String dest = destination;
if (!destination.endsWith(File.separator)) {
dest = destination + File.separator;
}
ZipFile file;
try {
file = null;
if (null == encoding)
file = new ZipFile(zipFile);
else
file = new ZipFile(zipFile, encoding);
Enumeration<ZipArchiveEntry> en = file.getEntries();
ZipArchiveEntry ze = null;
while (en.hasMoreElements()) {
ze = en.nextElement();
File f = new File(dest, ze.getName());
if (ze.isDirectory()) {
f.mkdirs();
continue;
} else {
f.getParentFile().mkdirs();
InputStream is = file.getInputStream(ze);
OutputStream os = new FileOutputStream(f);
IOUtils.copy(is, os);
is.close();
os.close();
fileNames.add(f.getAbsolutePath());
}
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileNames;
}
Aggregations