Right now I'm working on an archive browsing application that lets users navigate through archive contents, extract the archives and preview the files inside the archive. I'm using java.util.zip API. To preview a file I'm extracting it temporarily and opening it as a usual existing file. As you may understand, that's not a good approach since it won't preview files if there's not enough space to make a temporary extraction. Is there a working solution for passing ZipInputStream to an Activity to open it as a file? Is there another workaround for this problem? Thanks in advance.
In principle, you could create a ContentProvider that serves up the ZipInputStream.
In this sample project I demonstrate how to create a ContentProvider supporting openFile() that uses a pipe created by ParcelFileDescriptor.createPipe() to serve up a file. createPipe() returns a pair (two-element array) of ParcelFileDescriptors representing the ends of the pipe. You use the second element out of the array to write to via an OutputStream; openFile() returns the first element out of the array to be passed by Android to the calling process. The caller would use openInputStream() to read in what you transfer via the pipe.
In my case, I am sending an asset on which I get an InputStream via AssetManager. In your case, you would use your ZipInputStream.
Note that my sample project assumes it is being run on a device that has a PDF viewer, since it is serving a PDF out of assets and trying to open it via startActivity().
Related
Most of the solutions tells me use the File Class, but I am planning to use the audio stored in the Java Project. If I make an .exe file, would that work when I'm using File Class?
If you are using JavaFX, there is direct support for MP3. I just discovered this page, and haven't tried using it yet. I've always used javax.sound.sampled.SourceDataLine for audio output, and added libraries as needed to deal with the compression format. There are some useful libraries on github: https://github.com/pdudits/soundlibs. Of these, I've only used the jorbis library for ogg/vorbis encoded wav files. But the mp3 decoders here have been around for a long time, have been used in countless projects, and should work.
As far as packaging audio resources, a key thing to remember is that file systems don't "see into" jar files. So, a File address is basically useless as long as the resource is packed into a jar. But a URL can specify a file that is jarred. The usual practice is to have a "resource" folder for the project that can be specified by a relative address, and to load the resource using its URL. The URL can be obtained using the .getResource method of Class
For example, if you have a class named "AudioHandler" in your project in a package loction "com.dory.mymediaplayer", and a sub folder "/res", and an mp3 file "audiocue01.mp3" in /res, the line to obtain the URL for the mp3 file would be as follows:
URL url = AudioHandler.getClass().getResource("res/audiocue01.mp3");
However, depending on the needs of the library used for decoding the mp3, you might need to use the .getResourceAsStream method. The getResourceAsStream method returns an InputStream instead of a URL.
I have a Struts2 application with JPA. For this reason the conversion of the formats.
Actually, the Struts2 framework only needs an InputStream, but when I use a ByteArrayInputStream, the file is opened as a preview in the browser. I would like to make the file available for download, so I need a FileInputStream. (I've tried it with this type and it works, too.) I can't put a temporary file on the file system because I'm not allowed to do this.
I want to find a way to save captured picture and recorded sound in custom file-type(s) then open them in my app. I don't want to other apps could open my files (for example gallery app don't open my pictures).
Is there any way to encode and decode my files in my app for example by writing a string at end of files.
Thanks
There is no need for custom file types. If you save your files in Internal Storage, no other application can access them. You can also save files in the External Storage that are private, by calling getExternalFilesDir().
Details are here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Use a file extension no one else uses, then use this answer to open all such files with your app.
I'm trying to understand how to randomly traverse a file/files in a .tar.gz using TrueZIP in a Java 6 environment( using the Files classes). I found instances where it uses Java 7's Path, however, I can't come up with an example on how to randomly read an archive on Java 6.
Additionally, does "random" reading mean that it first uncompresses the entire archive, or does it read sections in the compressed file? The purpose is that I want to retrieve some basic information from the file without having to uncompress the entire thing just to read it(ie username).
The method that gzip uses to compress a file (especially .tar.gz files) usually implies that the output file is not random-accessible - you need the symbol table and other context from the entire file up to the current block to even be able to uncompress that block to see what's in it. This is one of the ways it achieves (somewhat) better compression over ZIP/pkzip, which compress each file individually before adding them to a container archive, resulting in the ability to seek to a specific file and uncompress just that file.
So, in order to pick a .tar.gz apart, you will need to uncompress the whole thing, either to a temporary file or in memory (if it's not too large), then you can jump to specific entries in the underlying .tar file, although that has to be done sequentially by skipping from header to header, as tar does not include a central index/directory of files.
I am not aware of TrueZip in particular, but at least in terms of Zip, RAR and Tar you can access single files and retrieve details about them and even extract them without touching the rest of the package.
Additionally, does "random" reading mean that it first uncompresses
the entire archive
If TrueZip follows Zip/RAR/Tar format, then it does not uncompress the entire archive.
The purpose is that I want to retrieve some basic information from the
file without having to uncompress the entire thing just to read it(ie
username).
As previously, that should be fine -- I don't know TrueZip API in particular, but file container formats allow you to inspect file info without reading a single bit of the data, and optionally extract/read the file contents without touching any other file in the container.
The source code comment of zran describes how such tools are working:
http://svn.ghostscript.com/ghostscript/tags/zlib-1.2.3/examples/zran.c
In conclusion one can say that the complete file has to be processed for generating the necessary index.
That is much faster than actually decompress everything.
The index allows to split the file into blocks that can be decompressed without having to decompress the blocks before. That is used for emulating random access.
So I'm writing a Spring web application that runs some scripts on the local server using ProcessBuilder. That part seems to be running fine. The scripts generate output files and then zip them up into a single .zip file. I'd like to provide the user with a way to download these files but I'm not sure of the best way to do this, or even how to implement any way to do it.
I tried putting the path to the directory into the URL, but I believe since Spring intercepts all URLs and it doesn't know how to process the one pointing at either the directory the Zip is in or the Zip itself, I just get an error. What would the proper way to do this be? Either to display a list of files or just link to the files themselves? Any help would be really appreciated, thank you.
The way you do any file download from a web-app is to write a servlet that writes the content of the file to the http response's output stream. There's an example of this here that's downloading an Excel file.
The directory listings that you get in a web-page don't just happen automatically, you'll have to write a JSP that displays the directory listing with hyperlinks on each file that link to file download servlet.
I looked around and could not find a complete directory listing example for spring. Though it is relatively simple to implement I put together a blog post to explain this, so that it could be quickly reused when needed http://krishna-passionatelycurious.blogspot.com/2013/04/file-download-page-using-spring.html.
But, the overall approach is to loop through the contents of a directory and generate appropriate links, such that the links point back to the same spring handler, where we could make a decision based on whether the accessed link actually points to a directory or a file and generate directory listing again or stream the content for download.
The blog has a sample implementation of this.