當前位置: 首頁>>代碼示例>>Java>>正文


Java IOUtils.copy方法代碼示例

本文整理匯總了Java中org.apache.commons.compress.utils.IOUtils.copy方法的典型用法代碼示例。如果您正苦於以下問題:Java IOUtils.copy方法的具體用法?Java IOUtils.copy怎麽用?Java IOUtils.copy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.commons.compress.utils.IOUtils的用法示例。


在下文中一共展示了IOUtils.copy方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: addFileToTarGz

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/**
 * Recursively adds a directory to a .tar.gz. Adapted from http://stackoverflow.com/questions/13461393/compress-directory-to-tar-gz-with-commons-compress
 *
 * @param tOut The .tar.gz to add the directory to
 * @param path The location of the folders and files to add
 * @param base The base path of entry in the .tar.gz
 * @throws IOException Any exceptions thrown during tar creation
 */
private void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    tOut.putArchiveEntry(tarEntry);
    Platform.runLater(() -> fileLabel.setText("Processing " + f.getPath()));

    if (f.isFile()) {
        FileInputStream fin = new FileInputStream(f);
        IOUtils.copy(fin, tOut);
        fin.close();
        tOut.closeArchiveEntry();
    } else {
        tOut.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}
 
開發者ID:wpilibsuite,項目名稱:java-installer,代碼行數:31,代碼來源:TarJREController.java

示例2: downloadFile

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/** 문자열 path를 가지고 파일을 다운 받아 바이트 배열로 바꿔주는 메서드.
 * https://stackoverflow.com/questions/2295221/java-net-url-read-stream-to-byte 소스 코드 참고
 * @param path
 * @return
 */
public static byte[] downloadFile(String path)
{
	try {
		URL url = new URL(path);
		URLConnection conn = url.openConnection();
		conn.setConnectTimeout(1000);
		conn.setReadTimeout(1000);
		conn.connect();

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		IOUtils.copy(conn.getInputStream(), baos);

		return baos.toByteArray();
	}
	catch (IOException e)
	{
		return null;
	}
}
 
開發者ID:forweaver,項目名稱:forweaver2.0,代碼行數:25,代碼來源:WebUtil.java

示例3: addTarGzipToArchive

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/**
 * given tar.gz file will be copied to this tar.gz file.
 * all files will be transferred to new tar.gz file one by one.
 * original directory structure will be kept intact
 *
 * @param tarGzipFile the archive file to be copied to the new archive
 */
public boolean addTarGzipToArchive(String tarGzipFile) {
  try {
    // construct input stream
    InputStream fin = Files.newInputStream(Paths.get(tarGzipFile));
    BufferedInputStream in = new BufferedInputStream(fin);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzIn);

    // copy the existing entries from source gzip file
    ArchiveEntry nextEntry;
    while ((nextEntry = tarInputStream.getNextEntry()) != null) {
      tarOutputStream.putArchiveEntry(nextEntry);
      IOUtils.copy(tarInputStream, tarOutputStream);
      tarOutputStream.closeArchiveEntry();
    }

    tarInputStream.close();
    return true;
  } catch (IOException ioe) {
    LOG.log(Level.SEVERE, "Archive File can not be added: " + tarGzipFile, ioe);
    return false;
  }
}
 
開發者ID:DSC-SPIDAL,項目名稱:twister2,代碼行數:31,代碼來源:TarGzipPacker.java

示例4: addFileToArchive

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/**
 * add one file to tar.gz file
 *
 * @param file file to be added to the tar.gz
 */
public boolean addFileToArchive(File file, String dirPrefixForTar) {
  try {
    String filePathInTar = dirPrefixForTar + file.getName();

    TarArchiveEntry entry = new TarArchiveEntry(file, filePathInTar);
    entry.setSize(file.length());
    tarOutputStream.putArchiveEntry(entry);
    IOUtils.copy(new FileInputStream(file), tarOutputStream);
    tarOutputStream.closeArchiveEntry();

    return true;
  } catch (IOException e) {
    LOG.log(Level.SEVERE, "File can not be added: " + file.getName(), e);
    return false;
  }
}
 
開發者ID:DSC-SPIDAL,項目名稱:twister2,代碼行數:22,代碼來源:TarGzipPacker.java

示例5: addFileToTarGz

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/**
 * This function copies a given file to the zip file
 *
 * @param tarArchiveOutputStream tar output stream of the zip file
 * @param file the file to insert to the zar file
 *
 * @throws IOException
 */
private static void addFileToTarGz(TarArchiveOutputStream tarArchiveOutputStream, File file)
                                        throws IOException
{
    String entryName =  file.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
    tarArchiveOutputStream.putArchiveEntry(tarEntry);

    if (file.isFile()) {

        try (FileInputStream input = new FileInputStream(file))
        {
            IOUtils.copy(input, tarArchiveOutputStream);
        }
        tarArchiveOutputStream.closeArchiveEntry();
    } else {//Directory
        System.out.println("The directory which need to be packed to tar folder cannot contain other directories");
    }
}
 
開發者ID:CheckPoint-APIs-Team,項目名稱:ShowPolicyPackage,代碼行數:27,代碼來源:TarGZUtils.java

示例6: testHandle

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/**
 * Test of handle method, of class ZipContentParser.
 */
@Test
public void testHandle() {
    System.out.println("handle");
    try {
        EntityManager manager = new PersistenceProvider().get();
        if(!manager.getTransaction().isActive()) {
            manager.getTransaction().begin();
        }
        manager.persist(new Modification("ZipContentParserTest.mod",31));
        manager.getTransaction().commit();
        IOUtils.copy(getClass().getResourceAsStream("/test.zip"), FileUtils.openOutputStream(new File(getAllowedFolder()+"/a.zip")));
        List <ProcessTask> result = get().handle(manager);
        Assert.assertTrue(
            "result is not of correct type",
            result instanceof List<?>
        );
        Assert.assertEquals(
            "Unexpected follow-ups",
            0,
            result.size()
        );
    } catch(Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
 
開發者ID:Idrinths-Stellaris-Mods,項目名稱:Mod-Tools,代碼行數:29,代碼來源:ZipContentParserTest.java

示例7: unpackTarArchiveEntry

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
public static void unpackTarArchiveEntry(TarArchiveInputStream tarStream, TarArchiveEntry entry, File outputDirectory) throws IOException {
    String fileName = entry.getName().substring(entry.getName().indexOf("/"), entry.getName().length());
    File outputFile = new File(outputDirectory,fileName);

    if (entry.isDirectory()) {
        if (!outputFile.exists()) {
            if (!outputFile.mkdirs()) {
                throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
            }
        }

        unpackTar(tarStream, entry.getDirectoryEntries(), outputFile);
        return;
    }

    OutputStream outputFileStream = new FileOutputStream(outputFile);
    IOUtils.copy(tarStream, outputFileStream);
    outputFileStream.close();
}
 
開發者ID:Panda-Programming-Language,項目名稱:Pandomium,代碼行數:20,代碼來源:ArchiveUtils.java

示例8: onPause

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
@Override
protected void onPause() {
    super.onPause();
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED &&
            !ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE))
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    0);

    // save file
    OutputStream f = null;
    try {
        f = new FileOutputStream(Utils.getBitcoinConf(this));
        IOUtils.copy(new ByteArrayInputStream(((EditText) findViewById(R.id.editText))
                .getText().toString().getBytes("UTF-8")), f);

    } catch (final IOException e) {
        Log.i(TAG, e.getMessage());
    } finally {
        IOUtils.closeQuietly(f);
    }
}
 
開發者ID:greenaddress,項目名稱:abcore,代碼行數:26,代碼來源:BitcoinConfEditActivity.java

示例9: extract

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
private static void extract(ZipArchiveEntry zipArchiveEntry, ZipFile zipFile, File outputDir) throws IOException {
    File extractedFile = new File(outputDir, zipArchiveEntry.getName());
    FileUtils.forceMkdir(extractedFile.getParentFile());
    if (zipArchiveEntry.isUnixSymlink()) {
        if (PosixUtil.isPosixFileStore(outputDir)) {
            logger.debug("Extracting [l] "+zipArchiveEntry.getName());
            String symlinkTarget = zipFile.getUnixSymlink(zipArchiveEntry);
            Files.createSymbolicLink(extractedFile.toPath(), new File(symlinkTarget).toPath());
        } else {
            logger.debug("Skipping ! [l] "+zipArchiveEntry.getName());
        }
    } else if (zipArchiveEntry.isDirectory()) {
        logger.debug("Extracting [d] "+zipArchiveEntry.getName());
        FileUtils.forceMkdir(extractedFile);
    } else {
        logger.debug("Extracting [f] "+zipArchiveEntry.getName());
        try (
                InputStream in = zipFile.getInputStream(zipArchiveEntry);
                OutputStream out = new FileOutputStream(extractedFile)
        ) {
            IOUtils.copy(in, out);
        }
    }
    updatePermissions(extractedFile, zipArchiveEntry.getUnixMode());
}
 
開發者ID:ctco,項目名稱:gradle-mobile-plugin,代碼行數:26,代碼來源:ZipUtil.java

示例10: unZipToFolder

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/**
 * 把一個ZIP文件解壓到一個指定的目錄中
 * @param zipfilename ZIP文件抽象地址
 * @param outputdir 目錄絕對地址
 */
public static void unZipToFolder(String zipfilename, String outputdir) throws IOException {
    File zipfile = new File(zipfilename);
    if (zipfile.exists()) {
        outputdir = outputdir + File.separator;
        FileUtils.forceMkdir(new File(outputdir));

        ZipFile zf = new ZipFile(zipfile, "UTF-8");
        Enumeration zipArchiveEntrys = zf.getEntries();
        while (zipArchiveEntrys.hasMoreElements()) {
            ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) zipArchiveEntrys.nextElement();
            if (zipArchiveEntry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputdir + zipArchiveEntry.getName() + File.separator));
            } else {
                IOUtils.copy(zf.getInputStream(zipArchiveEntry), FileUtils.openOutputStream(new File(outputdir + zipArchiveEntry.getName())));
            }
        }
    } else {
        throw new IOException("指定的解壓文件不存在:\t" + zipfilename);
    }
}
 
開發者ID:h819,項目名稱:spring-boot,代碼行數:26,代碼來源:CompressExample.java

示例11: makeOnlyZip

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
public static void makeOnlyZip() throws IOException, ArchiveException{
	File f1 = new File("D:/compresstest.txt");
       File f2 = new File("D:/test1.xml");
       
       final OutputStream out = new FileOutputStream("D:/中文名字.zip");
       ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
       
       os.putArchiveEntry(new ZipArchiveEntry(f1.getName()));
       IOUtils.copy(new FileInputStream(f1), os);
       os.closeArchiveEntry();

       os.putArchiveEntry(new ZipArchiveEntry(f2.getName()));
       IOUtils.copy(new FileInputStream(f2), os);
       os.closeArchiveEntry();
       os.close();
}
 
開發者ID:h819,項目名稱:spring-boot,代碼行數:17,代碼來源:CompressExample.java

示例12: addFilesToZip

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
void addFilesToZip(File source, File destination) throws IOException, ArchiveException {
    OutputStream archiveStream = new FileOutputStream(destination);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);
    Collection<File> fileList = FileUtils.listFiles(source, null, true);
    for (File file : fileList) {
        String entryName = getEntryName(source, file);
        ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
        archive.putArchiveEntry(entry);
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(input, archive);
        input.close();
        archive.closeArchiveEntry();
    }
    archive.finish();
    archiveStream.close();
}
 
開發者ID:spirylics,項目名稱:web2app,代碼行數:17,代碼來源:Npm.java

示例13: createPythonScript

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
private void createPythonScript() {
  ClassLoader classLoader = getClass().getClassLoader();
  File out = new File(scriptPath);

  if (out.exists() && out.isDirectory()) {
    throw new InterpreterException("Can't create python script " + out.getAbsolutePath());
  }

  try {
    FileOutputStream outStream = new FileOutputStream(out);
    IOUtils.copy(
        classLoader.getResourceAsStream("python/zeppelin_pyspark.py"),
        outStream);
    outStream.close();
  } catch (IOException e) {
    throw new InterpreterException(e);
  }

  logger.info("File {} created", scriptPath);
}
 
開發者ID:lorthos,項目名稱:incubator-zeppelin-druid,代碼行數:21,代碼來源:PySparkInterpreter.java

示例14: insertFile

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
private static void insertFile(File file, String entryName, ArchiveOutputStream archive) throws IOException {
    if (DEFAULT_EXCLUDES.contains(file.getName())) {
        logger.debug("Skipping ! [l] {}", entryName);
        return;
    }
    if (file.isFile()) {
        ZipArchiveEntry newEntry = new ZipArchiveEntry(entryName);
        setExtraFields(file.toPath(), UnixStat.FILE_FLAG, newEntry);
        archive.putArchiveEntry(newEntry);
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(input, archive);
        input.close();
        archive.closeArchiveEntry();
    } else {
        throw new IOException("Provided file is not a file");
    }
}
 
開發者ID:ctco,項目名稱:gradle-mobile-plugin,代碼行數:18,代碼來源:ZipUtil.java

示例15: createArchive

import org.apache.commons.compress.utils.IOUtils; //導入方法依賴的package包/類
/**
 * Create a gzipped tar archive containing the ProGuard/Native mapping files
 *
 * @param files array of mapping.txt files
 * @return the tar-gzipped archive
 */
private static File createArchive(List<File> files, String uuid) {
    try {
        File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                    new GZIPOutputStream(
                                new BufferedOutputStream(
                                            new FileOutputStream(tarZippedFile))));
        for (File file : files) {
            taos.putArchiveEntry(new TarArchiveEntry(file,
                    (uuid != null && !uuid.isEmpty() ? uuid : UUID.randomUUID()) + ".txt"));
            IOUtils.copy(new FileInputStream(file), taos);
            taos.closeArchiveEntry();
        }
        taos.finish();
        taos.close();
        return tarZippedFile;
    } catch (IOException e) {
        failWithError("IO Exception while trying to tar and zip the file.", e);
        return null;
    }
}
 
開發者ID:flurry,項目名稱:upload-clients,代碼行數:28,代碼來源:UploadMapping.java


注:本文中的org.apache.commons.compress.utils.IOUtils.copy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。