public void testCreateArchiveWithStreamTransformer() throws IOException {
    InputStreamTransformer is =
        new InputStreamTransformer() {
          @Nonnull
          public InputStream transform(
              @Nonnull PlexusIoResource resource, @Nonnull InputStream inputStream)
              throws IOException {
            return new BoundedInputStream(inputStream, 3);
          }
        };

    final ZipArchiver zipArchiver = getZipArchiver(getTestFile("target/output/all3bytes.zip"));
    File zipFIle = new File("src/test/resources/test.zip");
    DefaultArchivedFileSet afs = new DefaultArchivedFileSet(zipFIle);
    afs.setStreamTransformer(is);
    afs.setPrefix("azip/");
    zipArchiver.addArchivedFileSet(afs);

    DefaultFileSet dfs = new DefaultFileSet(new File("src/test/resources/mjar179"));
    dfs.setStreamTransformer(is);
    dfs.setPrefix("mj179/");
    zipArchiver.addFileSet(dfs);

    PlexusIoFileResourceCollection files = new PlexusIoFileResourceCollection();
    files.setBaseDir(new File("src/test/resources"));
    files.setStreamTransformer(is);
    files.setPrefix("plexus/");
    zipArchiver.addResources(files);

    zipArchiver.createArchive();
  }
  private void createArchive(ZipArchiver archiver) throws ArchiverException, IOException {
    archiver.createArchive();

    ZipFile zf = new ZipFile(archiver.getDestFile());

    Enumeration e = zf.getEntries();

    while (e.hasMoreElements()) {
      ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
      if (ze.isDirectory()) {
        if (ze.getName().startsWith("worldwritable")) {
          fileModeAssert(0777, UnixStat.PERM_MASK & ze.getUnixMode());
        } else if (ze.getName().startsWith("groupwritable")) {
          fileModeAssert(0070, UnixStat.PERM_MASK & ze.getUnixMode());
        } else {
          fileModeAssert(0500, UnixStat.PERM_MASK & ze.getUnixMode());
        }
      } else {
        if (ze.getName().equals("one.txt")) {
          fileModeAssert(0640, UnixStat.PERM_MASK & ze.getUnixMode());
        } else if (ze.getName().equals("two.txt")) {
          fileModeAssert(0664, UnixStat.PERM_MASK & ze.getUnixMode());
        } else if (ze.isUnixSymlink()) {
          //         assertEquals( ze.getName(), 0500, UnixStat.PERM_MASK & ze.getUnixMode() );
        } else {
          fileModeAssert(0400, UnixStat.PERM_MASK & ze.getUnixMode());
        }
      }
    }
  }
  public void testAddArchivedFileSet() throws Exception {
    File toBeAdded = new File("src/test/resources/test.zip");
    DefaultArchivedFileSet sfd = DefaultArchivedFileSet.archivedFileSet(toBeAdded);
    File zipFIle = getTestFile("target/output/withZip.zip");
    final ZipArchiver zipArchiver = getZipArchiver(zipFIle);
    InputStreamTransformer is =
        new InputStreamTransformer() {
          @Nonnull
          public InputStream transform(
              @Nonnull PlexusIoResource resource, @Nonnull InputStream inputStream)
              throws IOException {
            return new BoundedInputStream(inputStream, 3);
          }
        };
    sfd.setStreamTransformer(is);
    zipArchiver.addArchivedFileSet(sfd);
    zipArchiver.createArchive();

    final ZipUnArchiver zipUnArchiver = getZipUnArchiver(zipFIle);
    File destFile = new File("target/output/withZip");
    destFile.mkdirs();
    zipUnArchiver.setDestFile(destFile);
    zipUnArchiver.extract();
    File a3byteFile =
        new File(
            destFile,
            "Users/kristian/lsrc/plexus/plexus-archiver/src/main/java/org/codehaus/plexus/archiver/zip/ZipArchiver.java");
    assertTrue(a3byteFile.exists());
    assertTrue(a3byteFile.length() == 3);
  }
  public void testZipNonConcurrentResourceCollection() throws Exception {
    final File tarFile = getTestFile("target/output/zip-non-concurrent.tar");
    TarArchiver tarArchiver = (TarArchiver) lookup(Archiver.ROLE, "tar");
    tarArchiver.setDestFile(tarFile);
    // We're testing concurrency issue so we need large amount of files
    for (int i = 0; i < 100; i++) {
      tarArchiver.addFile(
          getTestFile("src/test/resources/manifests/manifest1.mf"), "manifest1.mf" + i);
      // Directories are added separately so let's test them too
      tarArchiver.addFile(
          getTestFile("src/test/resources/manifests/manifest2.mf"), "subdir" + i + "/manifest2.mf");
    }
    tarArchiver.createArchive();

    final File zipFile = new File("target/output/zip-non-concurrent.zip");
    ZipArchiver zipArchive = getZipArchiver(zipFile);
    zipArchive.addArchivedFileSet(tarFile, "prfx/");
    zipArchive.setEncoding("UTF-8");
    zipArchive.createArchive();

    final TarFile cmp1 = new TarFile(tarFile);
    final ZipFile cmp2 = new ZipFile(zipFile);
    ArchiveFileComparator.assertEquals(cmp1, cmp2, "prfx/");
    cmp1.close();
    cmp2.close();
  }
  public void testImplicitPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-implicit-dirmode.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);

    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);
    archiver.setFileMode(0222);
    archiver.addFile(new File("pom.xml"), "fizz/buzz/pom.xml");
    archiver.setDefaultDirectoryMode(0530);
    archiver.setDirectoryMode(-1); // Not forced mode
    archiver.setFileMode(0111);
    archiver.addFile(new File("pom.xml"), "fazz/bazz/pam.xml");
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("fizz/");
    assertEquals(040641, fizz.getUnixMode());
    ZipArchiveEntry pom = zf.getEntry("fizz/buzz/pom.xml");
    assertEquals(0100222, pom.getUnixMode());

    ZipArchiveEntry fazz = zf.getEntry("fazz/");
    assertEquals(040530, fazz.getUnixMode());
    ZipArchiveEntry pam = zf.getEntry("fazz/bazz/pam.xml");
    assertEquals(0100111, pam.getUnixMode());
  }
  public void testSymlinkArchivedFileSet() throws Exception {
    final File zipFile = getTestFile("src/test/resources/symlinks/symlinks.zip");
    final File zipFile2 = getTestFile("target/output/pasymlinks-archivedFileset.zip");
    final ZipArchiver zipArchiver = getZipArchiver(zipFile2);
    zipArchiver.addArchivedFileSet(zipFile);
    zipArchiver.createArchive();

    final ZipFile cmp1 = new ZipFile(zipFile);
    final ZipFile cmp2 = new ZipFile(zipFile2);
    ArchiveFileComparator.assertEquals(cmp1, cmp2, "");
  }
  public void notestJustThatOne() throws Exception {
    final File srcDir = new File("src");
    String[] inc = {"test/java/org/codehaus/plexus/archiver/zip/ZipShortTest.java"};
    final File zipFile = new File("target/output/zz1.zip");

    final File zipFile2 = new File("target/output/zz2.zip");
    ZipArchiver zipArchiver2 = getZipArchiver(zipFile2);

    // Bugbug: This does not work on 1.8....?
    zipArchiver2.addArchivedFileSet(zipFile);
    FileUtils.removePath(zipFile2.getPath());
    zipArchiver2.createArchive();
  }
  public void testForced() throws Exception {
    ZipArchiver archiver = newArchiver("archive2.zip");

    assertTrue(archiver.isForced());
    File f = archiver.getDestFile();
    if (f.exists()) {
      FileUtils.fileDelete(f.getPath());
    }
    assertFalse(f.exists());
    createArchive(archiver);
    long l1 = f.lastModified();
    assertTrue(f.exists());

    archiver = newArchiver("archive2.zip");
    waitUntilNewTimestamp(archiver.getDestFile(), l1);
    createArchive(archiver);
    long l2 = f.lastModified();
    assertTrue(f.exists());
    assertTrue(l2 > l1);

    archiver = newArchiver("archive2.zip");
    assertTrue(archiver.isSupportingForced());
    archiver.setForced(false);
    assertFalse(archiver.isForced());

    createArchive(archiver);
    long l3 = f.lastModified();
    assertTrue(f.exists());
    assertEquals(l2, l3);
  }
 public void testSymlinkZip() throws Exception {
   final File zipFile = getTestFile("target/output/pasymlinks.zip");
   final ZipArchiver zipArchiver = getZipArchiver(zipFile);
   PlexusIoFileResourceCollection files = new PlexusIoFileResourceCollection();
   files.setFollowingSymLinks(false);
   files.setBaseDir(new File("src/test/resources/symlinks"));
   files.setPrefix("plexus/");
   zipArchiver.addResources(files);
   zipArchiver.createArchive();
   final File output = getTestFile("target/output/unzipped");
   output.mkdirs();
   final ZipUnArchiver zipUnArchiver = getZipUnArchiver(zipFile);
   zipUnArchiver.setDestFile(output);
   zipUnArchiver.extract();
   File symDir = new File("target/output/unzipped/plexus/src/symDir");
   PlexusIoResourceAttributes fa = Java7FileAttributes.uncached(symDir);
   assertTrue(fa.isSymbolicLink());
 }
 @SuppressWarnings("ResultOfMethodCallIgnored")
 public void testSymlinkFileSet() throws Exception {
   final File zipFile = getTestFile("target/output/pasymlinks-fileset.zip");
   final ZipArchiver zipArchiver = getZipArchiver(zipFile);
   final DefaultFileSet fs = new DefaultFileSet();
   fs.setPrefix("bzz/");
   fs.setDirectory(new File("src/test/resources/symlinks/src"));
   zipArchiver.addFileSet(fs);
   zipArchiver.createArchive();
   final File output = getTestFile("target/output/unzipped/symlFs");
   output.mkdirs();
   final ZipUnArchiver zipUnArchiver = getZipUnArchiver(zipFile);
   zipUnArchiver.setDestFile(output);
   zipUnArchiver.extract();
   File symDir = new File(output, "bzz/symDir");
   PlexusIoResourceAttributes fa = Java7FileAttributes.uncached(symDir);
   assertTrue(fa.isSymbolicLink());
 }
  public void testCreateResourceCollection() throws Exception {
    final File srcDir = new File("src");
    final File zipFile = new File("target/output/src.zip");
    ZipArchiver zipArchiver = getZipArchiver(zipFile);
    zipArchiver.addDirectory(srcDir, null, FileUtils.getDefaultExcludes());
    zipArchiver.setEncoding("UTF-8");
    FileUtils.removePath(zipFile.getPath());
    zipArchiver.createArchive();

    final File zipFile2 = new File("target/output/src2.zip");
    ZipArchiver zipArchiver2 = getZipArchiver(zipFile2);
    zipArchiver2.addArchivedFileSet(zipFile, "prfx/");
    zipArchiver2.setEncoding("UTF-8");
    FileUtils.removePath(zipFile2.getPath());
    zipArchiver2.createArchive();

    final ZipFile cmp1 = new ZipFile(zipFile);
    final ZipFile cmp2 = new ZipFile(zipFile2);
    ArchiveFileComparator.assertEquals(cmp1, cmp2, "prfx/");
    cmp1.close();
    cmp2.close();
  }
  public void testOverddidenPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-overriden-modes.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);
    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);
    archiver.setFileMode(0777);
    archiver.addDirectory(new File("src/test/resources/symlinks/src"));
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("symDir");
    assertTrue(fizz.isUnixSymlink());
    ZipArchiveEntry symR = zf.getEntry("symR");
    assertTrue(symR.isUnixSymlink());
  }
  public void testForcedFileModes() throws IOException {
    File step1file = new File("target/output/forced-file-mode.zip");
    {
      final ZipArchiver zipArchiver = getZipArchiver(step1file);
      zipArchiver.setFileMode(0077);
      zipArchiver.setDirectoryMode(0007);
      PlexusIoResourceAttributes attrs =
          new SimpleResourceAttributes(123, "fred", 22, "filntstones", 0111);
      PlexusIoResource resource =
          ResourceFactory.createResource(
              new File("src/test/resources/folders/File.txt"), "Test.txt", null, attrs);
      zipArchiver.addResource(resource, "Test2.txt", 0707);
      PlexusIoFileResourceCollection files = new PlexusIoFileResourceCollection();
      files.setBaseDir(new File("src/test/resources/folders"));
      files.setPrefix("sixsixsix/");
      zipArchiver.addResources(files);

      zipArchiver.createArchive();

      ZipFile zf = new ZipFile(step1file);
      fileModeAssert(040007, zf.getEntry("sixsixsix/a/").getUnixMode());
      fileModeAssert(0100077, zf.getEntry("sixsixsix/b/FileInB.txt").getUnixMode());
      fileModeAssert(0100707, zf.getEntry("Test2.txt").getUnixMode());
      zf.close();
    }

    File Step2file = new File("target/output/forced-file-mode-from-zip.zip");
    {
      final ZipArchiver za2 = getZipArchiver(Step2file);
      za2.setFileMode(0666);
      za2.setDirectoryMode(0676);

      PlexusIoZipFileResourceCollection zipSrc = new PlexusIoZipFileResourceCollection();
      zipSrc.setFile(step1file);
      zipSrc.setPrefix("zz/");
      za2.addResources(zipSrc);
      za2.createArchive();
      ZipFile zf = new ZipFile(Step2file);
      fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode());
      fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode());
      zf.close();
    }

    File step3file = new File("target/output/forced-file-mode-from-zip2.zip");
    {
      final ZipArchiver za2 = getZipArchiver(step3file);
      za2.setFileMode(0666);
      za2.setDirectoryMode(0676);

      PlexusArchiverZipFileResourceCollection zipSrc =
          new PlexusArchiverZipFileResourceCollection();
      zipSrc.setFile(step1file);
      zipSrc.setPrefix("zz/");
      za2.addResources(zipSrc);
      za2.createArchive();
      ZipFile zf = new ZipFile(Step2file);
      fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode());
      fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode());
      zf.close();
    }
  }
 public void testDefaultUTF8withUTF8() throws IOException {
   final ZipArchiver zipArchiver = getZipArchiver(new File("target/output/utf8-with_utf.zip"));
   zipArchiver.setEncoding("UTF-8");
   zipArchiver.addDirectory(new File("src/test/resources/miscUtf8"));
   zipArchiver.createArchive();
 }
 private ZipArchiver getZipArchiver(File destFile) {
   final ZipArchiver zipArchiver = getZipArchiver();
   zipArchiver.setDestFile(destFile);
   return zipArchiver;
 }
  private ZipArchiver newArchiver(String name) throws Exception {
    ZipArchiver archiver = getZipArchiver(getTestFile("target/output/" + name));

    archiver.setFileMode(0640);
    archiver.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");
    archiver.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt", 0664);

    // reset default file mode for files included from now on
    archiver.setFileMode(0400);
    archiver.setDirectoryMode(0777);
    archiver.addDirectory(getTestFile("src/test/resources/world-writable/"), "worldwritable/");

    archiver.setDirectoryMode(0070);
    archiver.addDirectory(getTestFile("src/test/resources/group-writable/"), "groupwritable/");

    archiver.setDirectoryMode(0500);
    archiver.setFileMode(0400);
    archiver.addDirectory(getTestFile("src"));

    return archiver;
  }
  public void testCreateArchiveWithDetectedModes() throws Exception {

    String[] executablePaths = {"path/to/executable", "path/to/executable.bat"};

    String[] confPaths = {"path/to/etc/file", "path/to/etc/file2"};

    String[] logPaths = {"path/to/logs/log.txt"};

    int exeMode = 0777;
    int confMode = 0600;
    int logMode = 0640;

    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
      StackTraceElement e = new Throwable().getStackTrace()[0];
      System.out.println(
          "Cannot execute test: " + e.getMethodName() + " on " + System.getProperty("os.name"));
      return;
    }

    File tmpDir = null;
    try {
      tmpDir = File.createTempFile("zip-with-chmod.", ".dir");
      tmpDir.delete();

      tmpDir.mkdirs();

      for (String executablePath : executablePaths) {
        writeFile(tmpDir, executablePath, exeMode);
      }

      for (String confPath : confPaths) {
        writeFile(tmpDir, confPath, confMode);
      }

      for (String logPath : logPaths) {
        writeFile(tmpDir, logPath, logMode);
      }

      {
        Map<String, PlexusIoResourceAttributes> attributesByPath =
            PlexusIoResourceAttributeUtils.getFileAttributesByPath(tmpDir);
        for (String path : executablePaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + exeMode, exeMode, attrs.getOctalMode());
        }

        for (String path : confPaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + confMode,
              confMode,
              attrs.getOctalMode());
        }

        for (String path : logPaths) {
          PlexusIoResourceAttributes attrs = attributesByPath.get(path);
          if (attrs == null) {
            attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
          }

          assertNotNull(attrs);
          assertEquals(
              "Wrong mode for: " + path + "; expected: " + logMode, logMode, attrs.getOctalMode());
        }
      }

      File zipFile = getTestFile("target/output/zip-with-modes.zip");

      ZipArchiver archiver = getZipArchiver(zipFile);

      archiver.addDirectory(tmpDir);
      archiver.createArchive();

      assertTrue(zipFile.exists());

      File zipFile2 = getTestFile("target/output/zip-with-modes-L2.zip");

      archiver = getZipArchiver();
      archiver.setDestFile(zipFile2);

      archiver.addArchivedFileSet(zipFile);
      archiver.createArchive();

      ZipFile zf = new ZipFile(zipFile2);

      for (String path : executablePaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, mode);
      }

      for (String path : confPaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, mode);
      }

      for (String path : logPaths) {
        ZipArchiveEntry ze = zf.getEntry(path);

        int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

        assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, mode);
      }
    } finally {
      if (tmpDir != null && tmpDir.exists()) {
        try {
          FileUtils.forceDelete(tmpDir);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }