List of usage examples for java.util.zip ZipEntry getName
public String getName()
From source file:net.ftb.minecraft.MCInstaller.java
public static void launchMinecraft(String installDir, ModPack pack, LoginResponse resp, boolean isLegacy) { try {//from ww w. j ava2 s . co m File packDir = new File(installDir, pack.getDir()); String gameFolder = installDir + File.separator + pack.getDir() + File.separator + "minecraft"; File gameDir = new File(packDir, "minecraft"); File assetDir = new File(installDir, "assets"); File libDir = new File(installDir, "libraries"); File natDir = new File(packDir, "natives"); final String packVer = Settings.getSettings().getPackVer(pack.getDir()); Logger.logInfo("Setting up native libraries for " + pack.getName() + " v " + packVer + " MC " + pack.getMcVersion(packVer)); if (!gameDir.exists()) gameDir.mkdirs(); if (natDir.exists()) { natDir.delete(); } natDir.mkdirs(); if (isLegacy) extractLegacy(); Version base = JsonFactory.loadVersion(new File(installDir, "versions/{MC_VER}/{MC_VER}.json".replace("{MC_VER}", pack.getMcVersion(packVer)))); byte[] buf = new byte[1024]; for (Library lib : base.getLibraries()) { if (lib.natives != null) { File local = new File(libDir, lib.getPathNatives()); ZipInputStream input = null; try { input = new ZipInputStream(new FileInputStream(local)); ZipEntry entry = input.getNextEntry(); while (entry != null) { String name = entry.getName(); int n; if (lib.extract == null || !lib.extract.exclude(name)) { File output = new File(natDir, name); output.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(output); while ((n = input.read(buf, 0, 1024)) > -1) { out.write(buf, 0, n); } out.close(); } input.closeEntry(); entry = input.getNextEntry(); } } catch (Exception e) { ErrorUtils.tossError("Error extracting native libraries"); Logger.logError("", e); } finally { try { input.close(); } catch (IOException e) { } } } } List<File> classpath = new ArrayList<File>(); Version packjson = new Version(); if (!pack.getDir().equals("mojang_vanilla")) { if (isLegacy) { extractLegacyJson(new File(gameDir, "pack.json")); } if (new File(gameDir, "pack.json").exists()) { packjson = JsonFactory.loadVersion(new File(gameDir, "pack.json")); for (Library lib : packjson.getLibraries()) { //Logger.logError(new File(libDir, lib.getPath()).getAbsolutePath()); classpath.add(new File(libDir, lib.getPath())); } } } else { packjson = base; } if (!isLegacy) //we copy the jar to a new location for legacy classpath.add(new File(installDir, "versions/{MC_VER}/{MC_VER}.jar".replace("{MC_VER}", pack.getMcVersion(packVer)))); else { FileUtils .copyFile( new File(installDir, "versions/{MC_VER}/{MC_VER}.jar".replace("{MC_VER}", pack.getMcVersion(packVer))), new File(gameDir, "bin/" + Locations.OLDMCJARNAME)); FileUtils.killMetaInf(); } for (Library lib : base.getLibraries()) { classpath.add(new File(libDir, lib.getPath())); } Process minecraftProcess = MCLauncher.launchMinecraft(Settings.getSettings().getJavaPath(), gameFolder, assetDir, natDir, classpath, packjson.mainClass != null ? packjson.mainClass : base.mainClass, packjson.minecraftArguments != null ? packjson.minecraftArguments : base.minecraftArguments, packjson.assets != null ? packjson.assets : base.getAssets(), Settings.getSettings().getRamMax(), pack.getMaxPermSize(), pack.getMcVersion(packVer), resp.getAuth(), isLegacy); LaunchFrame.MCRunning = true; if (LaunchFrame.con != null) LaunchFrame.con.minecraftStarted(); StreamLogger.prepare(minecraftProcess.getInputStream(), new LogEntry().level(LogLevel.UNKNOWN)); String[] ignore = { "Session ID is token" }; StreamLogger.setIgnore(ignore); StreamLogger.doStart(); TrackerUtils.sendPageView(ModPack.getSelectedPack().getName() + " Launched", ModPack.getSelectedPack().getName()); try { Thread.sleep(1500); } catch (InterruptedException e) { } try { minecraftProcess.exitValue(); } catch (IllegalThreadStateException e) { LaunchFrame.getInstance().setVisible(false); LaunchFrame.setProcMonitor(ProcessMonitor.create(minecraftProcess, new Runnable() { @Override public void run() { if (!Settings.getSettings().getKeepLauncherOpen()) { System.exit(0); } else { if (LaunchFrame.con != null) LaunchFrame.con.minecraftStopped(); LaunchFrame launchFrame = LaunchFrame.getInstance(); launchFrame.setVisible(true); LaunchFrame.getInstance().getEventBus().post(new EnableObjectsEvent()); try { Settings.getSettings() .load(new FileInputStream(Settings.getSettings().getConfigFile())); LaunchFrame.getInstance().tabbedPane.remove(1); LaunchFrame.getInstance().optionsPane = new OptionsPane(Settings.getSettings()); LaunchFrame.getInstance().tabbedPane.add(LaunchFrame.getInstance().optionsPane, 1); LaunchFrame.getInstance().tabbedPane.setIconAt(1, LauncherStyle.getCurrentStyle() .filterHeaderIcon(this.getClass().getResource("/image/tabs/options.png"))); } catch (Exception e1) { Logger.logError("Failed to reload settings after launcher closed", e1); } } LaunchFrame.MCRunning = false; } })); } } catch (Exception e) { Logger.logError("Error while running launchMinecraft()", e); } }
From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java
/** * @param tmp//from w ww .j a va2s. co m * @param * @throws FileNotFoundException * @throws IOException */ private static void unzip(File file, File dir, PropertySetter ps, JProgressBar bar) throws FileNotFoundException, IOException { final int BUFFER = 2048; BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; int value = 0; while ((entry = zis.getNextEntry()) != null) { bar.setValue(value++); int count; byte data[] = new byte[BUFFER]; // write the files to the disk String outputFile = dir.getAbsolutePath() + File.separator + entry.getName(); if (entry.isDirectory()) { new File(outputFile).mkdirs(); } else { FileOutputStream fos = new FileOutputStream(outputFile); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); File oFile = new File(outputFile); if (OSInfosDefault.INSTANCE.getOs() == OperatingSystem.OSX) { // FIXME: we need to make everything executable as somehow // the executable bit is not preserved in // OSX oFile.setExecutable(true); } if (ps.filterFilename(oFile)) { ps.setProperty(outputFile); } } } zis.close(); file.delete(); }
From source file:com.idocbox.common.file.ZipExtracter.java
/** * extract given zip entry file from zip file * to given destrination directory./*ww w . java 2s .co m*/ * @param zf ZipFile object. * @param zipEntry zip entry to extract. * @param desDir destrination directory. * @param startDirLevel the level to start create directory. * Its value is 1,2,... * @throws IOException throw the exception whil desDir is no directory. */ private static void extract(final ZipFile zf, final ZipEntry zipEntry, final String desDir, final int... startDirLevel) throws IOException { //check destination directory. File desf = new File(desDir); if (!desf.exists()) { desf.mkdirs(); } int start = 1; if (null != startDirLevel && startDirLevel.length > 0) { start = startDirLevel[0]; if (start < 1) { start = 1; } } String startDir = ""; String zeName = zipEntry.getName(); String folder = zeName; boolean isDir = zipEntry.isDirectory(); if (null != folder) { String[] folders = folder.split("\\/"); if (null != folders && folders.length > 0) { int len = folders.length; if (start == 1) { startDir = zeName; } else { if (start > len) { //nothing to extract. } else { for (int i = start - 1; i < len; i++) { startDir += "/" + folders[i]; } if (null != startDir) { startDir = startDir.substring(1); } } } } } startDir = StringUtils.trim(startDir); if (StringUtils.isNotEmpty(startDir)) { StringBuilder desFileName = new StringBuilder(desDir); if (!desDir.endsWith("/") && !startDir.startsWith("/")) { desFileName.append("/"); } desFileName.append(startDir); File destFile = new File(desFileName.toString()); if (isDir) {//the entry is a dir. if (!destFile.exists()) { destFile.mkdirs(); } } else {//the entry is a file. File parentDir = new File(destFile.getParentFile().getPath()); if (!parentDir.exists()) { parentDir.mkdirs(); } //get entry input stream. InputStream is = zf.getInputStream(zipEntry); OutputStream os = new FileOutputStream(destFile); IOUtils.copy(is, os); if (null != is) { is.close(); } if (null != os) { os.close(); } } } }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
public static boolean removeZipEntry(File file, Pattern pattern, File targetFile) throws FileNotFoundException, IOException { byte[] buffer = new byte[1024]; java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(file); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile))); BufferedOutputStream bo = new BufferedOutputStream(out); InputStream inputStream;/*from ww w. jav a 2 s. c o m*/ Enumeration enumeration = zipFile.entries(); while (enumeration.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); String name = zipEntry.getName(); if (pattern.matcher(name).find()) { continue; } out.putNextEntry(zipEntry); inputStream = zipFile.getInputStream(zipEntry); write(inputStream, out, buffer); bo.flush(); } closeQuitely(zipFile); closeQuitely(out); closeQuitely(bo); return true; }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
public static void unzip(InputStream stream, ArchiveCallback callback, String... matches) { ZipInputStream in = new ZipInputStream(stream); ZipEntry entry; try {//w ww .ja v a2s. c om Map<String, File> skipped = new HashMap<String, File>(); matches = (String[]) ArrayUtils.add(matches, ".*"); while ((entry = in.getNextEntry()) != null) { String name = entry.getName(); if (!entry.isDirectory()) { if (name.contains("/") && !File.separator.equals("/")) name = name.replaceAll("/", File.separator + File.separator); if (name.contains("\\") && !File.separator.equals("\\")) name = name.replaceAll("\\\\", File.separator); String dir = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[0])) { callback.callback(dir, file, in); } else { File path = FileUtils.getTempFile(SecurityHelper.generateRandomSequence(16)); OutputStream out = FileUtils.openOutputStream(path); IOUtils.copy(in, out); IOUtils.closeQuietly(out); skipped.put(name, path); } } } for (int i = 1; i < matches.length; i++) { for (Iterator<Map.Entry<String, File>> iterator = skipped.entrySet().iterator(); iterator .hasNext();) { Map.Entry<String, File> next = iterator.next(); String name = next.getKey(); File path = next.getValue(); String dir = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[i])) { iterator.remove(); FileInputStream inputStream = FileUtils.openInputStream(path); callback.callback(dir, file, inputStream); IOUtils.closeQuietly(inputStream); FileUtils.forceDelete(path); } } } } catch (IOException e) { e.printStackTrace(); } }
From source file:com.hazelcast.stabilizer.Utils.java
public static void unzip(byte[] content, final File destinationDir) throws IOException { byte[] buffer = new byte[1024]; ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content)); ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File file = new File(destinationDir + File.separator + fileName); // log.finest("Unzipping: " + file.getAbsolutePath()); if (zipEntry.isDirectory()) { file.mkdirs();/*from w w w . j a v a 2 s . c o m*/ } else { file.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(file); try { int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } finally { closeQuietly(fos); } } zipEntry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java
public static InputStream getShapefileFromCompressed(InputStream is, ShpFileType type) { InputStream shapefile = null; ZipInputStream zis = new ZipInputStream(is); ZipEntry ze; try {//from w ww. j av a2 s .c om while ((ze = zis.getNextEntry()) != null) { if (!ze.isDirectory()) { String fileName = ze.getName().toLowerCase(); String baseShapefileName = type.toBase(fileName); if (baseShapefileName != null) { shapefile = zis; break; } } } } catch (IOException e) { try { zis.closeEntry(); } catch (IOException e2) { // TODO Auto-generated catch block e.printStackTrace(); } try { zis.close(); } catch (IOException e2) { // TODO Auto-generated catch block e.printStackTrace(); } } return shapefile; }
From source file:apim.restful.exportimport.utils.APIImportUtil.java
/** * This method decompresses the archive//from w w w . j a v a2s . com * * @param sourceFile the archive containing the API * @param destinationDirectory location of the archive to be extracted * @return extractedFolder the name of the zip */ public static String unzipArchive(File sourceFile, File destinationDirectory) throws APIManagementException { InputStream inputStream = null; FileOutputStream fileOutputStream = null; File destinationFile; ZipFile zipfile = null; String extractedFolder = null; try { zipfile = new ZipFile(sourceFile); Enumeration zipEntries = null; if (zipfile != null) { zipEntries = zipfile.entries(); } if (zipEntries != null) { int index = 0; while (zipEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipEntries.nextElement(); if (entry.isDirectory()) { //This index variable is used to get the extracted folder name; that is root directory if (index == 0) { //Get the folder name without the '/' character at the end extractedFolder = entry.getName().substring(0, entry.getName().length() - 1); } index = -1; new File(destinationDirectory, entry.getName()).mkdir(); continue; } inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); destinationFile = new File(destinationDirectory, entry.getName()); fileOutputStream = new FileOutputStream(destinationFile); copyStreams(inputStream, fileOutputStream); } } } catch (ZipException e) { log.error("Error in retrieving archive files."); throw new APIManagementException("Error in retrieving archive files.", e); } catch (IOException e) { log.error("Error in decompressing API archive files."); throw new APIManagementException("Error in decompressing API archive files.", e); } finally { try { if (zipfile != null) { zipfile.close(); } if (inputStream != null) { inputStream.close(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (IOException e) { log.error("Error in closing streams while decompressing files."); } } return extractedFolder; }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java
public static byte[] getShapeDataFromZip(byte[] localSourceData) { byte[] data = null; InputStream in = new ByteArrayInputStream(localSourceData); ZipInputStream zis = new ZipInputStream(in); ZipEntry ze; try {//from w w w . ja v a 2 s .com while ((ze = zis.getNextEntry()) != null) { if (!ze.isDirectory()) { String fileName = ze.getName().toLowerCase(); if (fileName.endsWith(".shp")) { long entrySize = ze.getSize(); if (entrySize != -1) { data = IOUtils.toByteArray(zis, entrySize); break; } } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { zis.closeEntry(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { zis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return data; }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
public static void editzip(InputStream stream, OutputStream outputStream, boolean close, EditArchiveCallback callback, String... matches) { ZipInputStream in = new ZipInputStream(stream); ZipOutputStream zout = new ZipOutputStream(outputStream); ZipEntry entry; try {/* w w w . j av a2s.c o m*/ Map<String, File> skipped = new HashMap<String, File>(); matches = (String[]) ArrayUtils.add(matches, ".*"); while ((entry = in.getNextEntry()) != null) { String name = entry.getName(); if (!entry.isDirectory()) { if (name.contains("/") && !File.separator.equals("/")) name = name.replaceAll("/", File.separator + File.separator); if (name.contains("\\") && !File.separator.equals("\\")) name = name.replaceAll("\\\\", File.separator); String dir = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[0])) { zout.putNextEntry(new ZipEntry(name)); callback.edit(dir, file, in, zout); zout.closeEntry(); } else { File path = FileUtils.getTempFile(SecurityHelper.generateRandomSequence(16)); OutputStream out = FileUtils.openOutputStream(path); IOUtils.copy(in, out); IOUtils.closeQuietly(out); skipped.put(name, path); } } } for (int i = 1; i < matches.length; i++) { for (Iterator<Map.Entry<String, File>> iterator = skipped.entrySet().iterator(); iterator .hasNext();) { Map.Entry<String, File> next = iterator.next(); String name = next.getKey(); File path = next.getValue(); String dir = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[i])) { iterator.remove(); FileInputStream inputStream = FileUtils.openInputStream(path); zout.putNextEntry(entry); callback.edit(dir, file, inputStream, zout); zout.closeEntry(); IOUtils.closeQuietly(inputStream); FileUtils.forceDelete(path); } } } while (callback.add(zout)) { zout.closeEntry(); } } catch (IOException e) { e.printStackTrace(); } finally { if (close) IOUtils.closeQuietly(zout); } }