Search in sources :

Example 6 with JBZipFile

use of com.intellij.util.io.zip.JBZipFile in project intellij-community by JetBrains.

the class ReorderJarsTest method testReordering.

@Test
public void testReordering() throws IOException {
    String path = getTestDataPath() + "/ide/plugins/reorderJars";
    JBZipFile zipFile1 = new JBZipFile(path + "/annotations.jar");
    try {
        List<JBZipEntry> entries = zipFile1.getEntries();
        System.out.println(entries);
    } finally {
        zipFile1.close();
    }
    ReorderJarsMain.main(new String[] { path + "/order.txt", path, myTempDirectory.getPath() });
    File[] files = myTempDirectory.listFiles();
    assertNotNull(files);
    assertEquals(1, files.length);
    File file = files[0];
    assertEquals("annotations.jar", file.getName());
    byte[] data;
    JBZipFile zipFile2 = new JBZipFile(file);
    try {
        List<JBZipEntry> entries = zipFile2.getEntries();
        System.out.println(entries);
        assertEquals(JarMemoryLoader.SIZE_ENTRY, entries.get(0).getName());
        JBZipEntry entry = entries.get(1);
        data = entry.getData();
        assertEquals(548, data.length);
        assertEquals("org/jetbrains/annotations/Nullable.class", entry.getName());
        assertEquals("org/jetbrains/annotations/NotNull.class", entries.get(2).getName());
        assertEquals("META-INF/", entries.get(3).getName());
    } finally {
        zipFile2.close();
    }
    ZipFile zipFile3 = new ZipFile(file);
    try {
        JarMemoryLoader loader = JarMemoryLoader.load(zipFile3, file.toURI().toURL(), null);
        assertNotNull(loader);
        Resource resource = loader.getResource("org/jetbrains/annotations/Nullable.class");
        assertNotNull(resource);
        byte[] bytes = resource.getBytes();
        assertEquals(548, bytes.length);
        assertTrue(Arrays.equals(data, bytes));
    } finally {
        zipFile3.close();
    }
}
Also used : JBZipFile(com.intellij.util.io.zip.JBZipFile) ZipFile(java.util.zip.ZipFile) JBZipFile(com.intellij.util.io.zip.JBZipFile) JBZipFile(com.intellij.util.io.zip.JBZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) JBZipEntry(com.intellij.util.io.zip.JBZipEntry) Test(org.junit.Test)

Example 7 with JBZipFile

use of com.intellij.util.io.zip.JBZipFile in project intellij-community by JetBrains.

the class WorkingContextManager method saveContext.

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(StringUtil.first(comment, 200, true));
        }
        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);
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) Element(org.jdom.Element) JBZipFile(com.intellij.util.io.zip.JBZipFile) IOException(java.io.IOException) JBZipEntry(com.intellij.util.io.zip.JBZipEntry)

Example 8 with JBZipFile

use of com.intellij.util.io.zip.JBZipFile in project intellij-community by JetBrains.

the class WorkingContextManager method loadContext.

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();
            loadContext(JdomKt.loadElement(new String(bytes)));
            return true;
        }
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        closeArchive(archive);
    }
    return false;
}
Also used : JBZipFile(com.intellij.util.io.zip.JBZipFile) InvalidDataException(com.intellij.openapi.util.InvalidDataException) IOException(java.io.IOException) WriteExternalException(com.intellij.openapi.util.WriteExternalException) JBZipEntry(com.intellij.util.io.zip.JBZipEntry)

Aggregations

JBZipEntry (com.intellij.util.io.zip.JBZipEntry)8 JBZipFile (com.intellij.util.io.zip.JBZipFile)8 IOException (java.io.IOException)5 File (java.io.File)4 InvalidDataException (com.intellij.openapi.util.InvalidDataException)2 WriteExternalException (com.intellij.openapi.util.WriteExternalException)2 ZipFile (java.util.zip.ZipFile)2 Element (org.jdom.Element)2 XMLOutputter (org.jdom.output.XMLOutputter)2 Test (org.junit.Test)2 Notification (com.intellij.notification.Notification)1 NotificationType (com.intellij.notification.NotificationType)1 Notifications (com.intellij.notification.Notifications)1 PathManager (com.intellij.openapi.application.PathManager)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Extensions (com.intellij.openapi.extensions.Extensions)1 Project (com.intellij.openapi.project.Project)1 FileUtil (com.intellij.openapi.util.io.FileUtil)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1