Search in sources :

Example 76 with ZipInputStream

use of java.util.zip.ZipInputStream in project processing by processing.

the class AppBundlerTask method copyResources.

private void copyResources(File resourcesDirectory) throws IOException {
    // Unzip res.zip into resources directory
    InputStream inputStream = getClass().getResourceAsStream("res.zip");
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    try {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            File file = new File(resourcesDirectory, zipEntry.getName());
            if (zipEntry.isDirectory()) {
                file.mkdir();
            } else {
                OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
                try {
                    int b = zipInputStream.read();
                    while (b != -1) {
                        outputStream.write(b);
                        b = zipInputStream.read();
                    }
                    outputStream.flush();
                } finally {
                    outputStream.close();
                }
            }
            zipEntry = zipInputStream.getNextEntry();
        }
    } finally {
        zipInputStream.close();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 77 with ZipInputStream

use of java.util.zip.ZipInputStream in project processing by processing.

the class Table method odsFindContentXML.

/**
   * Returns the next comma (not inside a quote) in the specified array.
   * @param c array to search
   * @param index offset at which to start looking
   * @return index of the comma, or -1 if line ended inside an unclosed quote
   */
/*
  static protected int nextComma(char[] c, int index) {
    if (index == c.length) {  // we're already at the end
      return c.length;
    }
    boolean quoted = c[index] == '\"';
    if (quoted) {
      index++; // step over the quote
    }
    for (int i = index; i < c.length; i++) {
      if (c[i] == '\"') {
        // if this fella started with a quote
        if (quoted) {
          if (i == c.length-1) {
            //return -1;  // ran out of chars
            // closing quote for field; last field on the line
            return c.length;
          } else if (c[i+1] == '\"') {
            // an escaped quote inside a quoted field, step over it
            i++;
          } else if (c[i+1] == ',') {
            // that's our closing quote, get outta here
            return i+1;
          }

        } else {  // not a quoted line
          if (i == c.length-1) {
            // we're at the end of the line, can't have an unescaped quote
            //return -1;  // ran out of chars
            throw new RuntimeException("Unterminated quoted field at end of line");
          } else if (c[i+1] == '\"') {
            // step over this crummy quote escape
            ++i;
          } else {
            throw new RuntimeException("Unterminated quoted field mid-line");
          }
        }
      } else if (!quoted && c[i] == ',') {
        return i;
      }
      if (!quote && (c[i] == ',')) {
        // found a comma, return this location
        return i;
      } else if (c[i] == '\"') {
        // if it's a quote, then either the next char is another quote,
        // or if this is a quoted entry, it better be a comma
        quote = !quote;
      }
    }
    // if still inside a quote, indicate that another line should be read
    if (quote) {
      return -1;
    }
    // made it to the end of the array with no new comma
    return c.length;
  }
  */
/**
   * Read a .ods (OpenDoc spreadsheet) zip file from an InputStream, and
   * return the InputStream for content.xml contained inside.
   */
private InputStream odsFindContentXML(InputStream input) {
    ZipInputStream zis = new ZipInputStream(input);
    ZipEntry entry = null;
    try {
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().equals("content.xml")) {
                return zis;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Example 78 with ZipInputStream

use of java.util.zip.ZipInputStream in project proxyee-down by monkeyWie.

the class FileUtil method unzip.

public static void unzip(String zipPath, String toPath, String... unzipFile) throws IOException {
    try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipPath))) {
        toPath = toPath == null ? new File(zipPath).getParent() : toPath;
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            final String entryName = entry.getName();
            if (entry.isDirectory() || (unzipFile != null && unzipFile.length > 0 && Arrays.stream(unzipFile).noneMatch((file) -> entryName.equalsIgnoreCase(file)))) {
                zipInputStream.closeEntry();
                continue;
            }
            File file = createFileSmart(toPath + File.separator + entryName);
            try (FileOutputStream outputStream = new FileOutputStream(file)) {
                byte[] bts = new byte[8192];
                int len;
                while ((len = zipInputStream.read(bts)) != -1) {
                    outputStream.write(bts, 0, len);
                }
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) Arrays(java.util.Arrays) ZipInputStream(java.util.zip.ZipInputStream) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) File(java.io.File) Stack(java.util.Stack) Method(java.lang.reflect.Method) ZipEntry(java.util.zip.ZipEntry) InputStream(java.io.InputStream) MappedByteBuffer(java.nio.MappedByteBuffer) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 79 with ZipInputStream

use of java.util.zip.ZipInputStream in project gocd by gocd.

the class CommandRepositoryInitializerIntegrationTest method shouldNotDeleteCustomCommandRepositoryWhenUpdatingDefaultCommandRepository.

@Test
public void shouldNotDeleteCustomCommandRepositoryWhenUpdatingDefaultCommandRepository() throws Exception {
    File versionFile = TestFileUtil.writeStringToTempFileInFolder("default", "version.txt", "10.1=10");
    File randomFile = TestFileUtil.createTestFile(versionFile.getParentFile(), "random");
    File defaultCommandRepoDir = versionFile.getParentFile();
    File customCommandRepoDir = TestFileUtil.createTestFolder(new File(defaultCommandRepoDir.getParent()), "customDir");
    File userFile = TestFileUtil.createTestFile(customCommandRepoDir, "userFile");
    initializer.usePackagedCommandRepository(new ZipInputStream(new FileInputStream(getZippedCommandRepo("12.4=12"))), defaultCommandRepoDir);
    assertThat(defaultCommandRepoDir.exists(), is(true));
    assertThat(FileUtils.readFileToString(new File(defaultCommandRepoDir, "version.txt"), UTF_8), is("12.4=12"));
    assertThat(new File(defaultCommandRepoDir, "snippet.xml").exists(), is(true));
    assertThat(new File(defaultCommandRepoDir, randomFile.getName()).exists(), is(false));
    assertThat(customCommandRepoDir.exists(), is(true));
    assertThat(new File(customCommandRepoDir, userFile.getName()).exists(), is(true));
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 80 with ZipInputStream

use of java.util.zip.ZipInputStream in project gocd by gocd.

the class CommandRepositoryInitializerIntegrationTest method shouldReplaceWithPackagedCommandRepositoryWhenOlderRepoExists.

@Test
public void shouldReplaceWithPackagedCommandRepositoryWhenOlderRepoExists() throws Exception {
    File versionFile = TestFileUtil.writeStringToTempFileInFolder("default", "version.txt", "10.1=10");
    File randomFile = TestFileUtil.createTestFile(versionFile.getParentFile(), "random");
    File defaultCommandRepoDir = versionFile.getParentFile();
    initializer.usePackagedCommandRepository(new ZipInputStream(new FileInputStream(getZippedCommandRepo("12.4=12"))), defaultCommandRepoDir);
    assertThat(defaultCommandRepoDir.exists(), is(true));
    assertThat(FileUtils.readFileToString(new File(defaultCommandRepoDir, "version.txt"), UTF_8), is("12.4=12"));
    assertThat(new File(defaultCommandRepoDir, "snippet.xml").exists(), is(true));
    assertThat(new File(defaultCommandRepoDir, randomFile.getName()).exists(), is(false));
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)968 ZipEntry (java.util.zip.ZipEntry)762 IOException (java.io.IOException)355 File (java.io.File)319 FileInputStream (java.io.FileInputStream)316 InputStream (java.io.InputStream)203 FileOutputStream (java.io.FileOutputStream)198 ByteArrayInputStream (java.io.ByteArrayInputStream)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 BufferedInputStream (java.io.BufferedInputStream)127 ZipOutputStream (java.util.zip.ZipOutputStream)91 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)80 OutputStream (java.io.OutputStream)67 URL (java.net.URL)58 Path (java.nio.file.Path)58 FileNotFoundException (java.io.FileNotFoundException)56 HashMap (java.util.HashMap)56 BufferedOutputStream (java.io.BufferedOutputStream)54 ZipFile (java.util.zip.ZipFile)43