Corrupt Excel Maven - java

I have some problems with Maven, Excel and poi package.
I access to an excel file thanks to the code :
Workbook workbook = WorkbookFactory.create(new File("src/main/resources/file.xlsx"));
Sheet sheet = workbook.getSheet(sheetName);
This code works correctly and I can read data inside later in my code.
Instead of a "new File(..)", I have to use this code below to access resources in dev mode and once the jar is built.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
String path = classLoader.getResource(fileName).toURI().getPath();
The given path is in "target/classes" and Maven do a "copy" of this file into folder "myproject/target/classes" of the current project(perfect so).
However, the xslx file copied by Maven is corrupted and neither by using Excel software, I can't access to its content. The original file size is 500Kb, the copied file size is more than 1Mb. (All other files img,txt.. are well copied excepted the xslx files)
I done lots of searches, I could find some answers like :
FileInputStream vs ClassPathResource vs getResourceAsStream and file integrity
. I tried all solutions I could find but impossible to solve mine and I always get the same error :
InvalidOperationException: Could not open the specified zip entry source stream
Or
java.io.FileNotFoundException: file.xlsx
From the same way of classLoader, I can access to my json, txt and image files.
Someone has answer on this issue ?
Why Maven doubles the size of the xlsx files and why they are corrupted ?
Any solution to solve that ?
I need help

Related

How can I retrieve CSV file inside of the Unzip folder?

I am trying to read and parsed all the CSV files, these files are inside of extracted zip file, but unfortunately, I'm only getting this (No such file or directory) - How can I get inside that folder and read those files? Take note that this folder is already unzipped.
I'm also thinking, that if that is not possible - is there any way that I can extract the zip file to files only, for example below:
Zip File: ZipFolder.zip
Expected: (No folder at all, automatically the files is on the target location already).
CSVFile1.csv
CSVFile2.csv
Can someone help me to get through this? your help is appreaciated.. Thank you!

RandomAccessFile to read files in a Jar file

I am having problem to use JWNL wordnet in a Jar file.
JWNL uses RandomAccessFile to read wordnet dictionary files. In order to create a Jar file, wordnet dictionary files are put in resources/wordnet folder. As resources is in my Build Path, I have no problem to run the application I created in Eclipse. However, when I use another application to run the created jar file, I get the following error:
java.io.FileNotFoundException: resources/wordnet/data.noun (No such file or directory)
from the following code:
RandomAccess _file = new RandomAccessFile(path, _permissions);
I use the following code to check the current working directory:
URL location = PrincetonRandomAccessDictionaryFile.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());
It seems both situation have the same location: /project/bin/
How should I fix the problem? Thank you
The key information you seem to be missing is that Jar files are compressed, and you can't "seek" because of the compression (which is I believe the DEFLATE algorithm).
However, you could extract the file(s) into temp file(s) on start and then use that. Temp files would be removed on application exit, and are the best answer I can think of.
RandomAccessFile to read files in a Jar file
There are no files in a JAR file. There are JAR entries. You can't read them with FileInputStreams, RandomAccessFiles, or FileReaders.You need to use a JarInputStream or its friends.

JAR - Listing files into a folder

I would like to get a list of file contained in a directory which is in a jar package.
I have an "images" folder, within it I have an Images class that should load all images from that directory.
In the past i used the MyClass.class.getResourceAsStream("filename"); to read files, but how do I read a directory?
This is what I tried:
System.out.println(Images.class.getResource("").getPath());
System.out.println(new File(Images.class.getResource("").getPath()).listFiles());
I tried with Images.class.getResource because I have to work with File and there isn't a constructor that accepts an InputStream.
The code produces
file:/home/k55/Java/MyApp/dist/Package.jar!/MyApp/images/
null
So it is finding the folder which I want to list files from, but it is not able to list files.
I've read on other forums that in fact you can't use this method for folders in a jar archive, so how can I accomplish this?
Update: if possible, i would like to read files without having to use the ZipInputStream
You can't do that easily.
What you need to do:
Get the path of the jar file.
Images.class.getResource("/something/that/exists").getPath()
Strip "!/something/that/exists".
Use Zip File System to browse the Jar file.
It's a little bit of hacking.

Input Stream returns null for mentioned file path

I am trying to access the sample_report.jrxml file from ReportUtil.java .
Following is the code to access the jrxml file :
InputStream in = new ReportUtil().getClass().getClassLoader().getResourceAsStream("resource/sample_report.jrxml");
I am getting in as NULL. I tried various combinations to read the jrxml file.
Can any one point correct way to get the file ?
Make an entry of resources folder in your classpath.
Assuming that your project is a maven project and you follow maven conventions, then
your file sample_report.jrxml residing in resources folder will be copied into the root level of the generated jar file.
For loading files you should use:
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream( "sample_report.jrxml" );
BalusC has explained this very well here
And as Brian Roach pointed out, there is a typo in the path to your file.

Java - read file from directory for jar

I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).

Categories

Resources