use of org.apache.tools.zip.ZipEntry in project hive by apache.
the class CompressionUtils method zip.
public static void zip(String parentDir, String[] inputFiles, String outputFile) throws IOException {
ZipOutputStream output = null;
try {
output = new ZipOutputStream(new FileOutputStream(new File(parentDir, outputFile)));
for (int i = 0; i < inputFiles.length; i++) {
File f = new File(parentDir, inputFiles[i]);
FileInputStream input = new FileInputStream(f);
output.putNextEntry(new ZipEntry(inputFiles[i]));
try {
IOUtils.copy(input, output);
} finally {
input.close();
}
}
} finally {
org.apache.hadoop.io.IOUtils.closeStream(output);
}
}
use of org.apache.tools.zip.ZipEntry in project wechat by dllwh.
the class ZipUtil method unZipFiles.
/**
* @Title: unZipFiles
* @Description: 解压文件到指定目录
* @param zipFile
* @param targetPath
* 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
* @throws Exception
*/
public static void unZipFiles(File zipFile, String targetPath) {
OutputStream os = null;
InputStream is = null;
ZipFile zip = null;
if (StringUtilHelper.isBlank(targetPath)) {
String zipPath = zipFile.getAbsolutePath();
targetPath = zipPath.substring(0, zipPath.lastIndexOf("."));
}
File pathFile = new File(targetPath);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
try {
zip = new ZipFile(zipFile);
for (Enumeration<?> entries = zip.getEntries(); entries.hasMoreElements(); ) {
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
String zipEntryName = zipEntry.getName();
InputStream in = zip.getInputStream(zipEntry);
String outPath = FilenameUtils.concat(targetPath, zipEntryName).replaceAll("\\*", "/");
outPath = new String(outPath.getBytes("utf-8"), "ISO8859-1");
// 判断路径是否存在,不存在则创建文件路径
File file = null;
if (outPath.indexOf("/") > 0) {
file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
} else {
file = new File(outPath.substring(0, outPath.lastIndexOf('\\')));
}
if (!file.exists()) {
file.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[BUFFEREDSIZE];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
in.close();
out.close();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (zip != null) {
zip.close();
}
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
} catch (Exception e) {
}
}
}
use of org.apache.tools.zip.ZipEntry in project gradle by gradle.
the class ZipFileTree method entriesSortedByName.
private Iterator<ZipEntry> entriesSortedByName(ZipFile zip) {
Map<String, ZipEntry> entriesByName = new TreeMap<>();
Enumeration<ZipEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
entriesByName.put(entry.getName(), entry);
}
return entriesByName.values().iterator();
}
use of org.apache.tools.zip.ZipEntry in project gradle by gradle.
the class ZipFileTree method visit.
@Override
public void visit(FileVisitor visitor) {
File zipFile = fileProvider.get();
if (!zipFile.exists()) {
throw new InvalidUserDataException(format("Cannot expand %s as it does not exist.", getDisplayName()));
}
if (!zipFile.isFile()) {
throw new InvalidUserDataException(format("Cannot expand %s as it is not a file.", getDisplayName()));
}
AtomicBoolean stopFlag = new AtomicBoolean();
File expandedDir = getExpandedDir();
try (ZipFile zip = new ZipFile(zipFile)) {
// The iteration order of zip.getEntries() is based on the hash of the zip entry. This isn't much use
// to us. So, collect the entries in a map and iterate over them in alphabetical order.
Iterator<ZipEntry> sortedEntries = entriesSortedByName(zip);
while (!stopFlag.get() && sortedEntries.hasNext()) {
ZipEntry entry = sortedEntries.next();
DetailsImpl details = new DetailsImpl(zipFile, expandedDir, entry, zip, stopFlag, chmod);
if (entry.isDirectory()) {
visitor.visitDir(details);
} else {
visitor.visitFile(details);
}
}
} catch (Exception e) {
throw new GradleException(format("Could not expand %s.", getDisplayName()), e);
}
}
use of org.apache.tools.zip.ZipEntry in project translationstudio8 by heartsome.
the class ZipFileExporter2 method addDbFolder.
public void addDbFolder(String destinationPath) throws IOException {
ZipEntry newEntry = new ZipEntry(destinationPath);
outputStream.putNextEntry(newEntry);
}
Aggregations