org.apache.tools.zip.ZipEntry Java Examples
The following examples show how to use
org.apache.tools.zip.ZipEntry.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileUtil.java From singleton with Eclipse Public License 2.0 | 6 votes |
/** * unzip zip file * * @param zipPath * @param targetDir * @throws IOException */ public static void unzipFiles(String zipPath, String targetDir) throws IOException { try (ZipFile zf = new ZipFile(new File(zipPath))) { Enumeration<ZipEntry> en = zf.getEntries(); while (en.hasMoreElements()) { ZipEntry ze = en.nextElement(); File f = new File(targetDir + ze.getName()); if (ze.isDirectory()) { f.mkdirs(); } else { writerUnzipFile(zf, ze, f); printInfoLog(ze.getName()); } } } catch (IOException e) { throw e; } printInfoLog("***********************Unzip Complete***************************"); }
Example #2
Source File: FileUtil.java From singleton with Eclipse Public License 2.0 | 6 votes |
private static void writerUnzipFile(ZipFile zf, ZipEntry ze, File f) throws IOException { int length = 0; byte[] b = new byte[2048]; if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } try (OutputStream outputStream = new FileOutputStream(f); InputStream inputStream = zf.getInputStream(ze)) { while ((length = inputStream.read(b)) > 0) { outputStream.write(b, 0, length); } } catch (IOException e) { throw e; } }
Example #3
Source File: ZipUtil.java From APICloud-Studio with GNU General Public License v3.0 | 6 votes |
public static void zip(String path, List<File> files) throws IOException { ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(path), 1024)); for(File f : files) { String zipName = f.getName(); if(!f.getName().contains(".png")) { zipName = f.getName() + ".xml"; } ZipEntry ze = new ZipEntry(zipName); ze.setTime(f.lastModified()); DataInputStream dis = new DataInputStream(new BufferedInputStream( new FileInputStream(f))); zos.putNextEntry(ze); int c; while ((c = dis.read()) != -1) { zos.write(c); } } zos.setEncoding("gbk"); zos.closeEntry(); zos.close(); }
Example #4
Source File: Files.java From development with Apache License 2.0 | 6 votes |
public void writeTo(OutputStream out) throws IOException { final Set<String> filenames = new HashSet<String>(); final ZipOutputStream zipout = new ZipOutputStream(out); for (IFile f : container.getFiles()) { assertNoAbsolutePath(f); assertNoDuplicates(filenames, f); ZipEntry entry = new ZipEntry(f.getLocalPath()); entry.setTime(f.getLastModified()); if (f.getPermissions() != IFile.UNDEF_PERMISSIONS) { entry.setUnixMode(f.getPermissions()); } zipout.putNextEntry(entry); f.writeTo(zipout); zipout.closeEntry(); } zipout.finish(); }
Example #5
Source File: FileUtils.java From frpMgr with MIT License | 5 votes |
/** * 将目录压缩到ZIP输出流 * @param dirPath 目录路径 * @param fileDir 文件信息 * @param zouts 输出流 */ public static void zipDirectoryToZipFile(String dirPath, File fileDir, ZipOutputStream zouts) { if (fileDir.isDirectory()) { File[] files = fileDir.listFiles(); // 空的文件夹 if (files.length == 0) { // 目录信息 ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir)); try { zouts.putNextEntry(entry); zouts.closeEntry(); } catch (Exception e) { e.printStackTrace(); } return; } for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { // 如果是文件,则调用文件压缩方法 FileUtils .zipFilesToZipFile(dirPath, files[i], zouts); } else { // 如果是目录,则递归调用 FileUtils.zipDirectoryToZipFile(dirPath, files[i], zouts); } } } }
Example #6
Source File: ZipUtil.java From jeecg with Apache License 2.0 | 5 votes |
/** * 解压文件到指定目录 * * @param zipFile * @param descDir * @author isea533 */ @SuppressWarnings("rawtypes") public static void unZipFiles(File zipFile, String descDir) throws IOException { File pathFile = new File(descDir); if (!pathFile.exists()) { pathFile.mkdirs(); } ZipFile zip = new ZipFile(zipFile); for (Enumeration entries = zip.getEntries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); String outPath = (descDir + zipEntryName).replaceAll("\\*", "/"); outPath = new String(outPath.getBytes("utf-8"), "ISO8859-1"); ; // 判断路径是否存在,不存在则创建文件路径 File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if (!file.exists()) { file.mkdirs(); } // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 if (new File(outPath).isDirectory()) { continue; } // 输出文件路径信息 //System.out.println(outPath); OutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while ((len = in.read(buf1)) > 0) { out.write(buf1, 0, len); } in.close(); out.close(); } }
Example #7
Source File: HttpDownHandler.java From AndroidWebServ with Apache License 2.0 | 5 votes |
/** 递归压缩文件进zip文件流 */ private void zip(ZipOutputStream zos, File file, String base) throws IOException { if (file.isDirectory()) { // 目录时 File[] files = file.listFiles(); if (null != files && files.length > 0) { for (File f : files) { zip(zos, f, base + "/" + f.getName()); // 递归 } } else { zos.putNextEntry(new ZipEntry(base + "/")); // 加入目录条目 zos.closeEntry(); } } else { zos.putNextEntry(new ZipEntry(base)); // 加入文件条目 FileInputStream fis = new FileInputStream(file); // 创建文件输入流 try { int count; // 读取计数 byte[] buffer = new byte[Config.BUFFER_LENGTH]; // 缓冲字节数组 /* 写入zip输出流 */ while ((count = fis.read(buffer)) != -1) { zos.write(buffer, 0, count); } } finally { zos.flush(); zos.closeEntry(); fis.close(); } } }
Example #8
Source File: Zipper.java From Lottery with GNU General Public License v2.0 | 5 votes |
public ZipEntry getZipEntry() { if (StringUtils.isBlank(parent)) { return null; } else { return new ZipEntry(parent); } }
Example #9
Source File: ZipUtil.java From APICloud-Studio with GNU General Public License v3.0 | 5 votes |
/** * Attempts to ensure the destination directory structure is generated. If there's a problem with write permissions, * a {@link CoreException} is thrown. * * @param entry * @param destinationPath * @throws CoreException */ private static void createDirectory(ZipEntry entry, File destinationPath) throws CoreException { String name = entry.getName(); File file = new File(destinationPath, name); if (entry.isDirectory()) { createDirectoryIfNecessary(file); } else if (name.indexOf('/') != -1) { createDirectoryIfNecessary(file.getParentFile()); } }
Example #10
Source File: ZipUtil.java From APICloud-Studio with GNU General Public License v3.0 | 5 votes |
/** * Open input stream for specified zip entry. * * @param zipFile * @param path * @return * @throws IOException */ public static InputStream openEntry(File zipFile, IPath path) throws IOException { ZipFile zip = new ZipFile(zipFile); ZipEntry entry = zip.getEntry(path.makeRelative().toPortableString()); if (entry != null) { return zip.getInputStream(entry); } return null; }
Example #11
Source File: ZipCopyAction.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitDir(FileCopyDetails dirDetails) { try { // Trailing slash in name indicates that entry is a directory ZipEntry archiveEntry = new ZipEntry(dirDetails.getRelativePath().getPathString() + '/'); archiveEntry.setTime(dirDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.DIR_FLAG | dirDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", dirDetails, zipFile), e); } }
Example #12
Source File: ZipCopyAction.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitFile(FileCopyDetails fileDetails) { try { ZipEntry archiveEntry = new ZipEntry(fileDetails.getRelativePath().getPathString()); archiveEntry.setTime(fileDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.FILE_FLAG | fileDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); fileDetails.copyTo(zipOutStr); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", fileDetails, zipFile), e); } }
Example #13
Source File: ZipCopyAction.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitDir(FileCopyDetails dirDetails) { try { // Trailing slash in name indicates that entry is a directory ZipEntry archiveEntry = new ZipEntry(dirDetails.getRelativePath().getPathString() + '/'); archiveEntry.setTime(dirDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.DIR_FLAG | dirDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", dirDetails, zipFile), e); } }
Example #14
Source File: ZipCopyAction.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitFile(FileCopyDetails fileDetails) { try { ZipEntry archiveEntry = new ZipEntry(fileDetails.getRelativePath().getPathString()); archiveEntry.setTime(fileDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.FILE_FLAG | fileDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); fileDetails.copyTo(zipOutStr); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", fileDetails, zipFile), e); } }
Example #15
Source File: ZipUtils.java From frpMgr with MIT License | 5 votes |
/** * 功能:执行文件压缩成zip文件 * * @param source * @param basePath * 待压缩文件根目录 * @param zos */ private static void zipFile(File source, String basePath, ZipOutputStream zos) { File[] files = new File[0]; if (source.isDirectory()) { files = source.listFiles(); } else { files = new File[1]; files[0] = source; } String pathName;// 存相对路径(相对于待压缩的根目录) byte[] buf = new byte[1024]; int length = 0; try { for (File file : files) { if (file.isDirectory()) { pathName = file.getPath().substring(basePath.length() + 1) + "/"; zos.putNextEntry(new ZipEntry(pathName)); zipFile(file, basePath, zos); } else { pathName = file.getPath().substring(basePath.length() + 1); InputStream is = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(is); zos.putNextEntry(new ZipEntry(pathName)); while ((length = bis.read(buf)) > 0) { zos.write(buf, 0, length); } is.close(); } } } catch (Exception e) { e.printStackTrace(); } }
Example #16
Source File: ZipUtils.java From joylau-springboot-daemon-windows with MIT License | 5 votes |
/** * <p> * 递归压缩文件 * </p> * * @param parentFile parentFile * @param basePath basePath * @param zos zos */ private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception { File[] files = new File[0]; if (parentFile.isDirectory()) { files = parentFile.listFiles(); } else { files = new File[1]; files[0] = parentFile; } String pathName; InputStream is; BufferedInputStream bis; byte[] cache = new byte[CACHE_SIZE]; for (File file : files) { if (file.isDirectory()) { pathName = file.getPath().substring(basePath.length() + 1) + "/"; zos.putNextEntry(new ZipEntry(pathName)); zipFile(file, basePath, zos); } else { pathName = file.getPath().substring(basePath.length() + 1); is = new FileInputStream(file); bis = new BufferedInputStream(is); zos.putNextEntry(new ZipEntry(pathName)); int nRead = 0; while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) { zos.write(cache, 0, nRead); } bis.close(); is.close(); } } }
Example #17
Source File: FileHelper.java From bird-java with MIT License | 5 votes |
/** * 递归压缩方法 * * @param sourceFile 源文件 * @param zos zip输出流 * @param name 压缩后的名称 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws Exception */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { byte[] buf = new byte[BUFFER_SIZE]; if (sourceFile.isFile()) { // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip输出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } // Complete the entry zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) { // 需要保留原来的文件结构时,需要对空文件夹进行处理 if (KeepDirStructure) { // 空文件夹的处理 zos.putNextEntry(new ZipEntry(name + "/")); // 没有文件,不需要文件的copy zos.closeEntry(); } } else { for (File file : listFiles) { // 判断是否需要保留原来的文件结构 if (KeepDirStructure) { // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } }
Example #18
Source File: ZipCopyAction.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitFile(FileCopyDetails fileDetails) { try { ZipEntry archiveEntry = new ZipEntry(fileDetails.getRelativePath().getPathString()); archiveEntry.setTime(fileDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.FILE_FLAG | fileDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); fileDetails.copyTo(zipOutStr); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", fileDetails, zipFile), e); } }
Example #19
Source File: ExtractLayer.java From netbeans with Apache License 2.0 | 5 votes |
private Source createSource(JarFile jf, java.util.zip.ZipEntry entry) { try { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); f.setValidating(false); DocumentBuilder b = f.newDocumentBuilder(); b.setEntityResolver(this); Document doc = b.parse(jf.getInputStream(entry)); return new DOMSource(doc); } catch (Exception ex) { throw new BuildException(ex); } }
Example #20
Source File: ZipCopyAction.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitDir(FileCopyDetails dirDetails) { try { // Trailing slash in name indicates that entry is a directory ZipEntry archiveEntry = new ZipEntry(dirDetails.getRelativePath().getPathString() + '/'); archiveEntry.setTime(dirDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.DIR_FLAG | dirDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", dirDetails, zipFile), e); } }
Example #21
Source File: ZipCopyAction.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitDir(FileCopyDetails dirDetails) { try { // Trailing slash in name indicates that entry is a directory ZipEntry archiveEntry = new ZipEntry(dirDetails.getRelativePath().getPathString() + '/'); archiveEntry.setTime(dirDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.DIR_FLAG | dirDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", dirDetails, zipFile), e); } }
Example #22
Source File: ZipCopyAction.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void visitFile(FileCopyDetails fileDetails) { try { ZipEntry archiveEntry = new ZipEntry(fileDetails.getRelativePath().getPathString()); archiveEntry.setTime(fileDetails.getLastModified()); archiveEntry.setUnixMode(UnixStat.FILE_FLAG | fileDetails.getMode()); zipOutStr.putNextEntry(archiveEntry); fileDetails.copyTo(zipOutStr); zipOutStr.closeEntry(); } catch (Exception e) { throw new GradleException(String.format("Could not add %s to ZIP '%s'.", fileDetails, zipFile), e); } }
Example #23
Source File: FileUtils.java From Shop-for-JavaWeb with MIT License | 5 votes |
/** * 将目录压缩到ZIP输出流 * @param dirPath 目录路径 * @param fileDir 文件信息 * @param zouts 输出流 */ public static void zipDirectoryToZipFile(String dirPath, File fileDir, ZipOutputStream zouts) { if (fileDir.isDirectory()) { File[] files = fileDir.listFiles(); // 空的文件夹 if (files.length == 0) { // 目录信息 ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir)); try { zouts.putNextEntry(entry); zouts.closeEntry(); } catch (Exception e) { e.printStackTrace(); } return; } for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { // 如果是文件,则调用文件压缩方法 FileUtils .zipFilesToZipFile(dirPath, files[i], zouts); } else { // 如果是目录,则递归调用 FileUtils.zipDirectoryToZipFile(dirPath, files[i], zouts); } } } }
Example #24
Source File: ZipUtil.java From APICloud-Studio with GNU General Public License v3.0 | 4 votes |
/** * Do the dirty work of actually extracting a {@link ZipEntry} to it's destination. * * @param zip * @param entry * @param destinationPath * @param transformer * @param conflicts * @param howToResolve * @param monitor * @return */ private static IStatus extractEntry(ZipFile zip, ZipEntry entry, File destinationPath, IInputStreamTransformer transformer, Conflict howToResolve, IProgressMonitor monitor) { // Return early since this is only supposed to handle files. if (entry.isDirectory()) { return Status.OK_STATUS; } SubMonitor subMonitor = SubMonitor.convert(monitor, 100); String name = entry.getName(); File file = new File(destinationPath, name); if (IdeLog.isInfoEnabled(CorePlugin.getDefault(), IDebugScopes.ZIPUTIL)) { IdeLog.logInfo(CorePlugin.getDefault(), MessageFormat.format("Extracting {0} as {1}", name, file.getAbsolutePath()), IDebugScopes.ZIPUTIL); //$NON-NLS-1$ } subMonitor.setTaskName(Messages.ZipUtil_extract_prefix_label + name); subMonitor.worked(2); try { if (file.exists()) { switch (howToResolve) { case OVERWRITE: if (IdeLog.isInfoEnabled(CorePlugin.getDefault(), IDebugScopes.ZIPUTIL)) { IdeLog.logInfo( CorePlugin.getDefault(), MessageFormat.format( "Deleting a file/directory before overwrite {0}", file.getAbsolutePath()), IDebugScopes.ZIPUTIL); //$NON-NLS-1$ } FileUtil.deleteRecursively(file); break; case SKIP: return Status.OK_STATUS; case PROMPT: return new Status(IStatus.INFO, CorePlugin.PLUGIN_ID, ERR_CONFLICTS, name, null); } } subMonitor.setWorkRemaining(95); extractFile(zip, entry, destinationPath, file, transformer, subMonitor.newChild(95)); } finally { subMonitor.done(); } return Status.OK_STATUS; }
Example #25
Source File: ZipFileExporter2.java From tmxeditor8 with GNU General Public License v2.0 | 4 votes |
public void write(IContainer container, String destinationPath) throws IOException { ZipEntry newEntry = new ZipEntry(destinationPath); outputStream.putNextEntry(newEntry); }
Example #26
Source File: ZipFileExporter2.java From translationstudio8 with GNU General Public License v2.0 | 4 votes |
public void addDbFile(File file, String destinationPath) throws IOException { ZipEntry newEntry = new ZipEntry(destinationPath); writeFile(newEntry, file); }
Example #27
Source File: ZipFileExporter2.java From translationstudio8 with GNU General Public License v2.0 | 4 votes |
public void addDbFolder(String destinationPath) throws IOException { ZipEntry newEntry = new ZipEntry(destinationPath); outputStream.putNextEntry(newEntry); }
Example #28
Source File: ZipFileExporter2.java From translationstudio8 with GNU General Public License v2.0 | 4 votes |
public void write(IContainer container, String destinationPath) throws IOException { ZipEntry newEntry = new ZipEntry(destinationPath); outputStream.putNextEntry(newEntry); }
Example #29
Source File: ZipUtil.java From APICloud-Studio with GNU General Public License v3.0 | 4 votes |
private static boolean isSymlink(ZipEntry entry) { return (entry.getUnixMode() & ATTR_SYMLINK) == ATTR_SYMLINK; }
Example #30
Source File: ZipFileTree.java From pushfish-android with BSD 2-Clause "Simplified" License | 4 votes |
public DetailsImpl(ZipEntry entry, ZipFile zip, AtomicBoolean stopFlag, Chmod chmod) { super(chmod); this.entry = entry; this.zip = zip; this.stopFlag = stopFlag; }