本文整理汇总了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 + "/");
}
}
}
}
示例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;
}
}
示例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;
}
}
示例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;
}
}
示例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");
}
}
示例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());
}
}
示例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();
}
示例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);
}
}
示例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());
}
示例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);
}
}
示例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();
}
示例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();
}
示例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);
}
示例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");
}
}
示例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;
}
}