1

嗨,我正在开发一个 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);
    }

createZipFile 方法:

     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);                             
        }
    }
}
4

0 回答 0