Java Path File Name nio storeFile(String fileName, InputStream inputStream, Path targetPath)

Here you can find the source of storeFile(String fileName, InputStream inputStream, Path targetPath)

Description

store File

License

Open Source License

Declaration

public static void storeFile(String fileName, InputStream inputStream, Path targetPath) throws IOException 

Method Source Code


//package com.java2s;
/*//  w  w w.  ja  va  2  s  .  co  m
 * Copyright (c) 2015-present Jan To?ovsk? <jan.tosovsky.cz@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipInputStream;

public class Main {
    public static void storeFile(String fileName, InputStream inputStream, Path targetPath) throws IOException {

        if (fileName == null) {
            fileName = targetPath.toString();
        }

        String extension = fileName.substring(fileName.lastIndexOf("."));

        switch (extension) {

        case ".gno": {

            try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {

                while (zipInputStream.getNextEntry() != null) {
                    storeFile(zipInputStream, targetPath);
                }
            }

            break;
        }

        case ".xml": {
            storeFile(inputStream, targetPath);
            break;
        }
        }
    }

    private static void storeFile(InputStream inputStream, Path filePath) throws IOException {

        try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(filePath))) {

            byte[] buffer = new byte[32 * 1024];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }
}

Related

  1. renameLogFile(Path original)
  2. saveFileFromByteArray(String filename, String pathDirectory, byte[] data)
  3. saveNamesToFile(Map namesMap, Path namesFile)
  4. saveToFile(String content, String directoryPath, String filename)
  5. shortenFileName(Path file, List dirs)
  6. subDirectoryNames(final Path dir)
  7. toClassName(String path)
  8. toResourcePath(String fileName)
  9. unzipFile(String fileName, String targetPath)