You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Alex Zeit <ze...@googlemail.com> on 2010/05/28 11:46:20 UTC

Creating and zipping binary files for download

Dear All,
I would like to create some binary files, zip them and start download in one
step.
I started with the functionality to create files depending on form input:

        form.add(new Button("button1", new Model("Download")) {
            @Override
            public void onSubmit() {
                new MyFileCreatror().createBinaryFile("myfile.bin")
            }
        });

Tomcat log:
May 28, 2010 10:50:08 AM java.util.prefs.FileSystemPreferences$2 run
WARNING: Couldn't create user preferences directory. User preferences are
unusable.
May 28, 2010 10:50:08 AM java.util.prefs.FileSystemPreferences$2 run
WARNING: java.io.IOException: No such file or directory


How do I define a place where files to be created?
Is there another approach to create and zip files?

Any help would be highly appreciated,
Alex

Re: Creating and zipping binary files for download

Posted by Alex Zeit <ze...@googlemail.com>.
Thank you very much for your suggestions. I was also thinking of creating
folders with random names.
I am using geotools libs to generate shapefiles set that I want to zip and
stream back.
I did not figure out yet how can I generate those files to stream using
Geotools.

2010/6/15 Jeremy Thomerson <je...@wickettraining.com>

> On Tue, Jun 15, 2010 at 5:24 AM, Alex Rass <ar...@itbsllc.com> wrote:
>
> > You should consider that 2 users can be doing it at the same time.
> > Which will lead to serious errors.
> > I would suggest using a random file name (or one with session hash in the
> > filename) so you avoid userA downloading userB's download.zip
> >
> > I would think that you should be able to just stream it right back to the
> > client as a redirect or smth. This temporary file thing seems very
> > improper.
> >
> > - Alex
> >
>
> You don't even have to do all that random naming stuff yourself.  Just use
> a
> Resource that's local to the user's session and Wicket will create the URL
> for you.  That also saves him from having to write it out to a file at all
> -
> he can just stream it back if desired.
>
> --
> Jeremy Thomerson
> http://www.wickettraining.com
>

Re: Creating and zipping binary files for download

Posted by Jeremy Thomerson <je...@wickettraining.com>.
On Tue, Jun 15, 2010 at 5:24 AM, Alex Rass <ar...@itbsllc.com> wrote:

> You should consider that 2 users can be doing it at the same time.
> Which will lead to serious errors.
> I would suggest using a random file name (or one with session hash in the
> filename) so you avoid userA downloading userB's download.zip
>
> I would think that you should be able to just stream it right back to the
> client as a redirect or smth. This temporary file thing seems very
> improper.
>
> - Alex
>

You don't even have to do all that random naming stuff yourself.  Just use a
Resource that's local to the user's session and Wicket will create the URL
for you.  That also saves him from having to write it out to a file at all -
he can just stream it back if desired.

-- 
Jeremy Thomerson
http://www.wickettraining.com

RE: Creating and zipping binary files for download

Posted by Alex Rass <ar...@itbsllc.com>.
You should consider that 2 users can be doing it at the same time.
Which will lead to serious errors.
I would suggest using a random file name (or one with session hash in the
filename) so you avoid userA downloading userB's download.zip

I would think that you should be able to just stream it right back to the
client as a redirect or smth. This temporary file thing seems very improper.

- Alex 

-----Original Message-----
From: Alex Zeit [mailto:zeitalex@googlemail.com] 
Sent: Sunday, May 30, 2010 7:30 AM
To: users@wicket.apache.org
Subject: Re: Creating and zipping binary files for download

Thank you very much Jeremy for your help. As you suggested I create
temporary dir and place files there.
Wicket is really great! The solution to my problem was just in couple of
lines of code:

    org.apache.wicket.util.file.File wdir = new
org.apache.wicket.util.file.File(createTempDirectory());
    ZipResourceStream zrs = new ZipResourceStream(wdir);
    ResourceStreamRequestTarget target = new
ResourceStreamRequestTarget(zrs, "download.zip");
    RequestCycle.get().setRequestTarget(target);


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Creating and zipping binary files for download

Posted by Alex Zeit <ze...@googlemail.com>.
Thank you very much Jeremy for your help. As you suggested I create
temporary dir and place files there.
Wicket is really great! The solution to my problem was just in couple of
lines of code:

    org.apache.wicket.util.file.File wdir = new
org.apache.wicket.util.file.File(createTempDirectory());
    ZipResourceStream zrs = new ZipResourceStream(wdir);
    ResourceStreamRequestTarget target = new
ResourceStreamRequestTarget(zrs, "download.zip");
    RequestCycle.get().setRequestTarget(target);

Re: Creating and zipping binary files for download

Posted by Jeremy Thomerson <je...@wickettraining.com>.
On Fri, May 28, 2010 at 4:46 AM, Alex Zeit <ze...@googlemail.com> wrote:

> Dear All,
> I would like to create some binary files, zip them and start download in
> one
> step.
> I started with the functionality to create files depending on form input:
>
>        form.add(new Button("button1", new Model("Download")) {
>            @Override
>            public void onSubmit() {
>                new MyFileCreatror().createBinaryFile("myfile.bin")
>            }
>        });
>
> Tomcat log:
> May 28, 2010 10:50:08 AM java.util.prefs.FileSystemPreferences$2 run
> WARNING: Couldn't create user preferences directory. User preferences are
> unusable.
> May 28, 2010 10:50:08 AM java.util.prefs.FileSystemPreferences$2 run
> WARNING: java.io.IOException: No such file or directory
>
>
> How do I define a place where files to be created?
> Is there another approach to create and zip files?
>
> Any help would be highly appreciated,
> Alex
>

This has nothing to do with Wicket - it's just simple java file permissions.
 And we would have to see the code in your file creator (or,
"MyFileCreatror") to help.  But it's something specific to your file
permissions - and possibly tomcat config to allow writing to certain
directories.  You should probably try to write to the temp directory with
File.createTemp.....

-- 
Jeremy Thomerson
http://www.wickettraining.com