Code example for File

Methods: getParentFile

0
    assertZipContains(output, "subdir/example.txt");
  } 
 
  @Test 
  public void testShouldZipMultipleDirectories() throws IOException { 
    touch(new File(inputDir, "subdir/example.txt"));
    touch(new File(inputDir, "subdir2/fishy/food.txt"));
 
    File output = new File(outputDir, "subdir.zip");
    zip.zip(inputDir, output);
 
    assertTrue(output.exists());
    assertZipContains(output, "subdir/example.txt");
    assertZipContains(output, "subdir2/fishy/food.txt");
  } 
 
  @Test 
  public void testCanUnzipASingleEntry() throws IOException { 
    File source = InProject.locate(
        "java/client/test/org/openqa/selenium/internal/single-file.zip"); 
 
    zip.unzip(source, outputDir);
 
    assertTrue(new File(outputDir, "example.txt").exists());
  } 
 
  @Test 
  public void testCanUnzipAComplexZip() throws IOException { 
    File source = InProject.locate(
        "java/client/test/org/openqa/selenium/internal/subfolders.zip"); 
 
    zip.unzip(source, outputDir);
 
    assertTrue(new File(outputDir, "example.txt").exists());
    assertTrue(new File(outputDir, "subdir/foodyfun.txt").exists());
  } 
 
  @Test 
  public void testWillNotOverwriteAnExistingZip() { 
    try { 
      zip.zip(inputDir, outputDir);
      fail("Should have thrown an exception"); 
    } catch (IOException e) {
      assertTrue(e.getMessage(), e.getMessage().contains("already exists"));
    } 
  } 
 
  @Test 
  public void testCanZipASingleFile() throws IOException { 
    File input = new File(inputDir, "foo.txt");
    File unwanted = new File(inputDir, "nay.txt");
    touch(input);
    touch(unwanted);
 
    String zipped = zip.zipFile(inputDir, input);
 
    zip.unzip(zipped, outputDir);
    File unzipped = new File(outputDir, "foo.txt");
    File notThere = new File(outputDir, "nay.txt");
 
    assertTrue(unzipped.exists());
    assertFalse(notThere.exists());
  } 
 
  @Test 
  public void testZippingASingleFileWillThrowIfInputIsNotAFile() throws IOException { 
    try { 
      zip.zipFile(inputDir.getParentFile(), inputDir);
      fail("Should have failed"); 
    } catch (IllegalArgumentException ignored) {
    } 
  } 
 
  private void assertZipContains(File output, String s) throws IOException {
    FileInputStream fis = new FileInputStream(output);
    ZipInputStream zis = new ZipInputStream(fis);
    try { 
      ZipEntry entry;
      while ((entry = zis.getNextEntry()) != null) {
        if (s.equals(entry.getName().replaceAll("\\\\", "/"))) {
          return; 
        } 
      } 
    } finally { 
      zis.close();
    } 
 
    fail("File not in zip: " + s);
  } 
 
  private void touch(File file) throws IOException {
    File parent = file.getParentFile();
    if (!parent.exists()) {
      assertTrue(parent.mkdirs());
    } 
    FileOutputStream fos = new FileOutputStream(file);
    fos.write("".getBytes());
Connect your IDE to all the code out there  Get Codota for Java