Java Path File Write nio writeFileToArchive(final ZipOutputStream zos, final Path baseSrcPath, final Path filePath)

Here you can find the source of writeFileToArchive(final ZipOutputStream zos, final Path baseSrcPath, final Path filePath)

Description

write File To Archive

License

Apache License

Declaration

private static void writeFileToArchive(final ZipOutputStream zos, final Path baseSrcPath, final Path filePath)
            throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;
import java.io.InputStream;

import java.nio.file.Files;
import java.nio.file.Path;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    private static final String CONFIG_NAMES_FILE_TOC = "toc.html";

    private static void writeFileToArchive(final ZipOutputStream zos, final Path baseSrcPath, final Path filePath)
            throws IOException {
        final byte[] buffer = new byte[1024];
        final String zipPath = baseSrcPath.resolve(extractLanguageRelativePath(filePath)).toString();
        final ZipEntry ze = new ZipEntry(zipPath);
        zos.putNextEntry(ze);/*  www. j  a va 2 s  . co  m*/
        try (InputStream in = Files.newInputStream(filePath);) {
            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
        }
        zos.closeEntry();
    }

    private static String extractLanguageRelativePath(final Path absolutePath) {
        final String fileName;
        if (absolutePath != null && absolutePath.getFileName() != null) {
            fileName = absolutePath.getFileName().toString();
        } else {
            fileName = "";
        }
        final String pathRegexSeparator;
        if ("\\".equals(File.separator)) {
            pathRegexSeparator = "\\\\";
        } else {
            pathRegexSeparator = File.separator;
        }
        final String languagePathPattern = "(it|en|fr)" + pathRegexSeparator + "(faq|imgs|pages|pdf|toc\\.html)";
        Pattern pattern = Pattern.compile(languagePathPattern);
        final String pathString;
        if (absolutePath != null) {
            pathString = absolutePath.toString();
        } else {
            pathString = "";
        }
        Matcher matcher = pattern.matcher(pathString);
        String lang = "";
        String folderType = "";
        while (matcher.find()) {
            lang = matcher.group(1);
            folderType = matcher.group(2);
        }
        String result = lang + File.separator + folderType;
        if (result.endsWith(File.separator)) {
            result = fileName;
        } else if (!fileName.equals(CONFIG_NAMES_FILE_TOC)) {
            result += File.separator + fileName;
        }
        return result;
    }
}

Related

  1. writeAudio(String source, String writePath)
  2. writeEndStringInFile(Path path, String s)
  3. writeFile(String filePath, String text)
  4. writeFile(String path, String output)
  5. writeFile(String payload, Path balOutPath)
  6. writeFully(Path filePath, byte[] bytes)
  7. writeJaasConfig(File krbDir, String princ, String keytabPath)
  8. writeJson(Object data, Path path)
  9. writeLastReadDate(String sinceDate, Path sinceFile)