@Nullable public static Course getCourse(String zipFilePath) { try { final JBZipFile zipFile = new JBZipFile(zipFilePath); final JBZipEntry entry = zipFile.getEntry(EduNames.COURSE_META_FILE); if (entry == null) { return null; } byte[] bytes = entry.getData(); final String jsonText = new String(bytes, CharsetToolkit.UTF8_CHARSET); Gson gson = new GsonBuilder() .registerTypeAdapter(Task.class, new StudySerializationUtils.Json.TaskAdapter()) .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create(); return gson.fromJson(jsonText, Course.class); } catch (IOException e) { LOG.error("Failed to unzip course archive"); LOG.error(e); } return null; }
@Test public void testPluginXml() throws Exception { String path = getTestDataPath() + "/ide/plugins/reorderJars"; ReorderJarsMain.main(new String[] { path + "/zkmOrder.txt", path, myTempDirectory.getPath() } ); File[] files = myTempDirectory.listFiles(); assertNotNull(files); File file = files[0]; assertEquals("zkm.jar", file.getName()); JBZipFile zipFile = new JBZipFile(file); try { List<JBZipEntry> entries = zipFile.getEntries(); System.out.println(entries); assertEquals(JarMemoryLoader.SIZE_ENTRY, entries.get(0).getName()); assertEquals("META-INF/plugin.xml", entries.get(1).getName()); } finally { zipFile.close(); } }
private synchronized void saveContext(@Nullable String entryName, String zipPostfix, @Nullable String comment) { JBZipFile archive = null; try { archive = getTasksArchive(zipPostfix); if (entryName == null) { int i = archive.getEntries().size(); do { entryName = "context" + i++; } while (archive.getEntry("/" + entryName) != null); } JBZipEntry entry = archive.getOrCreateEntry("/" + entryName); if (comment != null) { entry.setComment(comment); } Element element = new Element("context"); saveContext(element); String s = new XMLOutputter().outputString(element); entry.setData(s.getBytes(CharsetToolkit.UTF8_CHARSET)); } catch (IOException e) { LOG.error(e); } finally { closeArchive(archive); } }
private synchronized boolean loadContext(String zipPostfix, String entryName) { JBZipFile archive = null; try { archive = getTasksArchive(zipPostfix); JBZipEntry entry = archive.getEntry(StringUtil.startsWithChar(entryName, '/') ? entryName : "/" + entryName); if (entry != null) { byte[] bytes = entry.getData(); Document document = JDOMUtil.loadDocument(new String(bytes)); Element rootElement = document.getRootElement(); loadContext(rootElement); return true; } } catch (Exception e) { LOG.error(e); } finally { closeArchive(archive); } return false; }
private synchronized List<ContextInfo> getContextHistory(String zipPostfix) { JBZipFile archive = null; try { archive = getTasksArchive(zipPostfix); List<JBZipEntry> entries = archive.getEntries(); return ContainerUtil.mapNotNull(entries, new NullableFunction<JBZipEntry, ContextInfo>() { public ContextInfo fun(JBZipEntry entry) { return entry.getName().startsWith("/context") ? new ContextInfo(entry.getName(), entry.getTime(), entry.getComment()) : null; } }); } catch (IOException e) { LOG.error(e); return Collections.emptyList(); } finally { closeArchive(archive); } }
private void removeContext(String name, String postfix) { JBZipFile archive = null; try { archive = getTasksArchive(postfix); JBZipEntry entry = archive.getEntry(name); if (entry != null) { archive.eraseEntry(entry); } } catch (IOException e) { LOG.error(e); } finally { closeArchive(archive); } }
private synchronized void pack(int max, int delta, String zipPostfix) { JBZipFile archive = null; try { archive = getTasksArchive(zipPostfix); List<JBZipEntry> entries = archive.getEntries(); if (entries.size() > max + delta) { JBZipEntry[] array = entries.toArray(new JBZipEntry[entries.size()]); Arrays.sort(array, ENTRY_COMPARATOR); for (int i = array.length - 1; i >= max; i--) { archive.eraseEntry(array[i]); } } } catch (IOException e) { LOG.error(e); } finally { closeArchive(archive); } }
private synchronized void saveContext(@Nullable String entryName, String zipPostfix, @Nullable String comment) { JBZipFile archive = null; try { archive = getTasksArchive(zipPostfix); if (entryName == null) { int i = archive.getEntries().size(); do { entryName = "context" + i++; } while (archive.getEntry("/" + entryName) != null); } JBZipEntry entry = archive.getOrCreateEntry("/" + entryName); if (comment != null) { entry.setComment(comment); } Element element = new Element("context"); saveContext(element); String s = new XMLOutputter().outputString(element); entry.setData(s.getBytes("UTF-8")); } catch (IOException e) { LOG.error(e); } finally { closeArchive(archive); } }
private static void closeArchive(JBZipFile archive) { if (archive != null) { try { archive.close(); } catch (IOException e) { LOG.error(e); } } }
private static JBZipFile getOrCreateZipFile(File archiveFile) throws IOException { FileUtil.createIfDoesntExist(archiveFile); return new JBZipFile(archiveFile); }