Java:从FTP下载.Zip文件并提取内容,而不将文件保存在本地系统上

Java:从FTP下载.Zip文件并提取内容,而不将文件保存在本地系统上,java,hadoop,ftp,zip,Java,Hadoop,Ftp,Zip,我有一个要求,我需要从FTP服务器下载某些.Zip文件,并将存档的内容(内容是一些XML文件)推送到HDFS(Hadoop分布式文件系统)。因此,到目前为止,我正在使用acpache-FTPClient连接到FTP服务器,并首先将文件下载到本地计算机。稍后将其解压缩,并将文件夹路径提供给一个方法,该方法将迭代本地文件夹并将文件推送到HDFS。为了便于理解,我还在下面附加了一些代码片段 //Gives me an active FTPClient FTPClient ftpCilent

我有一个要求,我需要从FTP服务器下载某些.Zip文件,并将存档的内容(内容是一些XML文件)推送到HDFS(Hadoop分布式文件系统)。因此,到目前为止,我正在使用acpache-FTPClient连接到FTP服务器,并首先将文件下载到本地计算机。稍后将其解压缩,并将文件夹路径提供给一个方法,该方法将迭代本地文件夹并将文件推送到HDFS。为了便于理解,我还在下面附加了一些代码片段

 //Gives me an active FTPClient
    FTPClient ftpCilent = getActiveFTPConnection();
    ftpCilent.changeWorkingDirectory(remoteDirectory);

    FTPFile[] ftpFiles = ftpCilent.listFiles();
    if(ftpFiles.length <= 0){
    logger.info("Unable to find any files in given location!!");
    return;
    }
    //Iterate files
    for(FTPFile eachFTPFile : ftpFiles){
        String ftpFileName = eachFTPFile.getName();

        //Skips files if not .zip files
        if(!ftpFileName.endsWith(".zip")){
           continue;
        }

    System.out.println("Reading File -->" + ftpFileName);
    /*
     * location is the path on local system given by user
     * usually loaded by a property file.
     *
     * Create a archiveLocation where archived files are
     * downloaded from FTP.
     */
    String archiveFileLocation = location + File.separator + ftpFileName;
    String localDirName = ftpFileName.replaceAll(".zip", "");
    /*
     * localDirLocation is the location where a folder is created
     * by the name of the archive in the FTP and the files are copied to
     * respective folders.
     *
     */
    String localDirLocation = location + File.separator + localDirName;
    File localDir = new File(localDirLocation);
    localDir.mkdir();

    File archiveFile = new File(archiveFileLocation);

    FileOutputStream archiveFileOutputStream = new FileOutputStream(archiveFile);

    ftpCilent.retrieveFile(ftpFileName, archiveFileOutputStream);
    archiveFileOutputStream.close();

    //Delete the archive file after coping it's contents
    FileUtils.forceDeleteOnExit(archiveFile);

    //Read the archive file from archiveFileLocation.       
    ZipFile zip = new ZipFile(archiveFileLocation);
    Enumeration entries = zip.entries();

    while(entries.hasMoreElements()){
    ZipEntry entry = (ZipEntry)entries.nextElement();

    if(entry.isDirectory()){
        logger.info("Extracting directory " + entry.getName());
        (new File(entry.getName())).mkdir();
        continue;
    }

    logger.info("Extracting File: " + entry.getName());
    IOUtils.copy(zip.getInputStream(entry), new FileOutputStream(
    localDir.getAbsolutePath() + File.separator + entry.getName()));
    }

    zip.close();
   /*
    * Iterates the folder location provided and load the files to HDFS
    */    
    loadFilesToHDFS(localDirLocation);
    }
    disconnectFTP();
//给我一个活动的FTP客户端
FTPClient ftpCilent=getActiveFTPConnection();
ftpCilent.changeWorkingDirectory(远程目录);
FTPFile[]ftpFiles=ftpCilent.listFiles();
如果(ftpFiles.length使用压缩流。
请看这里:


具体请参见此处的代码示例1。

ZipInputStream
?同样,这是2016年,因此避免使用
文件
…使用
文件
路径
而不是@fge,我本可以使用
ZipInputStream
,但它将
InputStream
作为输入,从
FTPClient
写入
OutputStream
。因此,我在写入
FileOutputStream
和读取与
InputStream
相同的数据时面临时间延迟。对这种情况有什么建议吗?请查看
FTPClient
的文档……有一种方法可以满足您的需求:它被称为
。retrieveFileStream()
。嘿@fge非常感谢您的投入。效果很好。!!