将ZipOutputStream写入阻止连接的OutPutStream

时间:2012-09-23 12:39:22

标签: java android apache-httpcomponents

您好我正在开发一个android文件资源管理器应用程序。当请求下载大文件夹时,我正在使用下面的代码压缩文件夹并将其写入输出流。但是在文件下载之前,任何进一步的请求都会被阻止。可能是什么问题?我应该考虑将其移到线程中吗? ?我已经尝试了很多变化,似乎没有任何变化。正确方向的任何指针都将非常感激。

        if(f.isDirectory()&& action.equalsIgnoreCase("download")){
        File zip=new File(file);

        entity = new EntityTemplate(new ContentProducer() {
            public void writeTo(final OutputStream outstream) throws IOException {
                action=null;
                OutputStreamWriter writer=fileop.createZipFile(file, outstream);
                writer.flush();
            }
        });response.setHeader("Content-Type", "application/zip");
        response.setHeader("Content-Disposition","attachment; filename=\"" +zip.getName()+".zip\"");
        response.setEntity(entity);
    }

createZipFileMethod:

     OutputStreamWriter createZipFile(String sourceDir,OutputStream os) {
    // TODO Auto-generated method stub
    OutputStreamWriter osw=null;
        try
    {
        //wrap object of OutputStream
        osw=new OutputStreamWriter(os,"UTF-8");                     
        //create object of ZipOutputStream from OutputStream
        ZipOutputStream zout = new ZipOutputStream(os);

        //create File object from source directory
        File fileSource = new File(sourceDir);

        addDirectory(zout, fileSource);

        //close the ZipOutputStream
        zout.close();

        System.out.println("Zip file has been created!");

    }
    catch(IOException ioe)
    {
        System.out.println("IOException :" + ioe);     
    }

    return osw;

}

private static void addDirectory(ZipOutputStream zout, File fileSource) {

    //get sub-folder/files list
    File[] files = fileSource.listFiles();

    System.out.println("Adding directory " + fileSource.getName());

    for(int i=0; i < files.length; i++)
    {
        //if the file is directory, call the function recursively
        if(files[i].isDirectory())
        {
            addDirectory(zout, files[i]);
            continue;
        }

        /*
         * we are here means, its file and not directory, so
         * add it to the zip file
         */

        try
        {
            System.out.println("Adding file " + files[i].getName());

            //create byte buffer
            byte[] buffer = new byte[1024];

            //create object of FileInputStream
            FileInputStream fin = new FileInputStream(files[i]);

            zout.putNextEntry(new ZipEntry(files[i].getName()));

            /*
             * After creating entry in the zip file, actually
             * write the file.
             */
            int length;

            while((length = fin.read(buffer)) > 0)
            {
                zout.write(buffer, 0, length);
            }

            /*
             * After writing the file to ZipOutputStream, use
             *
             * void closeEntry() method of ZipOutputStream class to
             * close the current entry and position the stream to
             * write the next entry.
             */

            zout.closeEntry();

            //close the InputStream
            fin.close();

        }
        catch(IOException ioe)
        {
            System.out.println("IOException :" + ioe);                             
        }
    }
}

0 个答案:

没有答案