/*
 * Copyright 2015 John "LuaMilkshake" Marion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.pktr.smartbackup.creator;

import net.minecraft.command.ICommandSender;

/**
 * Handles creation of archives.
 *
 * <p>An archive is a complete file archive (eg tarball or zip file) of the data being backed
 * up.</p>
 */
public class ArchiveCreator extends BackupCreator {
  /**
   * Sets up an archive creation thread.
   *
   * @param sender The {@link ICommandSender} that requested this archive. Used for status messages.
   */
  public ArchiveCreator(ICommandSender sender) {
    super(sender);
    this.setName("Archive Thread");
  }

  /** {@inheritDoc} */
  @Override
  public String getBackupType() {
    return "archive";
  }

  @Override
  protected void createBackup(){
    // TODO: Create archive
  }
}

/*
 * Copyright 2015 John "LuaMilkshake" Marion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.pktr.smartbackup.creator;

import net.minecraft.command.ICommandSender;

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.EnumSet;
import java.util.TimeZone;

/**
 * Handles creation of archives.
 *
 * <p>An archive is a complete file archive (eg tarball or zip file) of the data being backed
 * up.</p>
 */
public class ArchiveCreator extends BackupCreator {
  /** ArrayList loaded from config file of files/folders to leave out */
  private ArrayList<String> backupExcludes;
  /** File to write the archive into */
  private Path archiveOutput;

  /**
   * Sets up an archive creation thread.
   *
   * @param sender The {@link ICommandSender} that requested this archive. Used for status messages.
   */
  public ArchiveCreator(ICommandSender sender) {
    super(sender);
    this.setName("Archive Thread");
  }

  /** Provides methods to use with {@link Files#walkFileTree} for archives */
  private class ArchiveVisitor implements FileVisitor<Path> {
    @Override
    public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attr)
        throws IOException {
      // Ignore the whole directory if it's in the ignore list
      if (backupExcludes.contains(path.toString())) {
        return FileVisitResult.SKIP_SUBTREE;
      }

      return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path path, BasicFileAttributes attr) throws IOException {
      // Continue without doing anything with the file if it's in the ignore list
      if (backupExcludes.contains(path.toString())) {
        return FileVisitResult.CONTINUE;
      }

      // TODO Add this file to the archive

      return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path path, IOException exception) throws IOException {
      throw exception;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path path, IOException exception) throws IOException {
      return FileVisitResult.CONTINUE;
    }
  }

  /** {@inheritDoc} */
  @Override
  public String getBackupType() {
    return "archive";
  }

  /** {@inheritDoc} */
  @Override
  protected void createBackup() throws InterruptedException, IOException {
    SimpleDateFormat rfc8601Formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH_mm_ss'Z'");
    rfc8601Formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    archiveOutput = Paths.get(config.getBackupOutputDir(),
        rfc8601Formatter.format(new Date()) + ".zip");

    if (Files.exists(archiveOutput)) {
      throw new FileAlreadyExistsException("Archive output file already exists: " +
          archiveOutput.toString());
    }

    // TODO Might need to ensure that the output dir exists before we try to write a file to it

    backupExcludes = new ArrayList<>(Arrays.asList(config.getBackupExcludes()));

    for (String file : config.getBackupIncludes()) {
      Files.walkFileTree(Paths.get(file), EnumSet.allOf(FileVisitOption.class),
          Integer.MAX_VALUE, new ArchiveVisitor());
    }
  }
}

Add ArchiveVisitor and basic archive creation code