Teemo
Teemo

Reputation: 459

Java IO File Prefix string too short - but its not

Trying to create simple .tar.gz file from one directory. There is my code:

File destinationFile = new File("/var/www/swOfflineFeeds/Companies/2/")
File sourceFile = new File("/var/www/swOfflineFeeds/Companies/2/64cacf30-b294-49f4-b166-032a808d73cd/")
println("destinationFile exists: " + destinationFile.exists()) //prints true
println("sourceFile exists: " + sourceFile.exists()) //prints true

Archiver arch = ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP)
File archiveFile = arch.create("64cacf30-b294-49f4-b166-032a808d73cd", destinationFile, sourceFile)

And I am getting error message:

| Error 2018-02-15 12:47:08,925 [http-bio-8183-exec-1] ERROR errors.GrailsExceptionResolver  - IllegalArgumentException occurred when processing request: [GET] /socialwall/test/index
Prefix string too short. Stacktrace follows:
Message: Prefix string too short
    Line | Method
->> 1978 | createTempFile in java.io.File
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     51 | create         in org.rauschig.jarchivelib.ArchiverCompressorDecorator
|     19 | index . . . .  in com.manas.socialwall.TestController$$EQjisHuy
|    198 | doFilter       in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter . . . in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker      in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run            in java.lang.Thread

As you can see filename is correct. I googled and some people mention bug in Java IO File class. Is this true ? How to avoid this problem?

Upvotes: 5

Views: 8048

Answers (1)

AxelH
AxelH

Reputation: 14572

Checking the library code used we see that the create method look like :

public File create(String archive, File destination, File... sources) throws IOException {
     ...
     File temp = File.createTempFile(destination.getName(), archiver.getFilenameExtension(), destination);

The prefix is the first parameter. If you check what File.getName() do :

Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname's name sequence. If the pathname's name sequence is empty, then the empty string is returned.

In your case.

File destinationFile = new File("/var/www/swOfflineFeeds/Companies/2/");
System.out.println(destinationFile.getName());

2

The prefix received is too short for the temp file to create, it expect a prefixe of at least 3 character. See File.createTempFile

prefix - The prefix string to be used in generating the file's name; must be at least three characters long

In your case, it seems you just provide a folder, provide the file name instead (with at least 3 character).


An issue has been created by Michael.

Upvotes: 7

Related Questions