I made a program and I created an export function for documents (entries). The procedure: The user has favorited documents. There exist 2-3 strategies (BibTex,RIS,HTML) a user can choose to export his documents. For each strateg开发者_如何学Pythony a new .zip file is being generated with all documents inside. The created .zip archives are sent to the user via email.
For me ( Windows ) it works great. I can extract those archives without any problems. But a friend of my, who is using Mac, gets errors while extracting them and I do not know why.
Here the important code:
for ( String strategy : strategies ) {
// Coderedundanz
// Jede Strategie benötigt eigene Parameter
if (strategy.equals("BibTex")) {
_zipName = "ezdl_export_bibtex";
_fileExtension = ".bib";
_strategy = csf.bibTex;
}
else if (strategy.equals("RIS")) {
_zipName = "ezdl_export_ris";
_fileExtension = ".ris";
_strategy = csf.ris;
}
else if (strategy.equals("HTML")) {
_zipName = "ezdl_export_html";
_fileExtension = ".html";
_strategy = csf.html;
}
else {
_zipName = _zipExtension = "";
_fileExtension = "";
_strategy = null;
}
// Gibt es eine korrekte Strategie?
if ( !_zipName.equals("") && !_fileExtension.equals("") && _strategy != null) {
// 1. .zip Datei generieren
// 2. Für jedes TextDocument eine eigene Datei erstellen
// 3. Datei in die .zip Datei einfügen
// 4. .zip Datei schließen und in die E-Mail hinzufügen
File file = File.createTempFile(_zipName + _zipExtension, ".tmp");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
out.setLevel(6);
for ( TextDocument document : documents ) {
out.putNextEntry(new ZipEntry( document.getOid() + _fileExtension));
String temp = _strategy.print ( (TextDocument) document).asString().toString();
out.write( temp.getBytes() );
out.closeEntry();
}
out.finish();
out.close();
PreencodedMimeBodyPart part_x = new PreencodedMimeBodyPart("base64");
part_x.setFileName(_zipName + _zipExtension);
part_x.setContent(new String(Base64Coder.encode( getBytesFromFile (file))), "text/plain");
multi.addBodyPart(part_x);
if (file.exists())
file.delete();
You see for each strategy an own archive is being created. The program loops through the documents (TextDocument) and with _strategy.print you get a String as output.
As I said.. for me it works great, but not on Mac. Are there any differences? I guess.. .zip is .zip. Or should I create tarballs (.tar.gz) for Mac?
EDIT:
serena:tmp3 alex$ unzip ezdl_export_bibtex.zip Archive: ezdl_export_bibtex.zip End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. note: ezdl_export_bibtex.zip may be a plain executable, not an archive unzip: cannot find zipfile directory in one of ezdl_export_bibtex.zip or ezdl_export_bibtex.zip.zip, and cannot find
Here is a screen: http://img3.imageshack.us/i/ziperror.png. It shows the error: "Unable to unarchive - Error - 1 - Operation not permitted"
I also changed my code to:
out.write( temp.getBytes() );
out.flush();
out.closeEntry();
But still the same problem.
Although it doesn't address your problem directly, you can verify the integrity of the zip
file using the -t
option on the command line.
$ unzip -t java-puzzlers.zip | tail -1 No errors detected in compressed data of java-puzzlers.zip.
In addition, you can examine the parent directory's permissions, walking up the path until you see a problem.
$ ls -ld .. drwxr-xr-x@ 26 trashgod staff 884 Jan 17 2010 .. $ ls -ld ../.. drwx------+ 23 trashgod staff 782 Dec 17 17:15 ../..
Addendum: If it "has something to do with encoding," I always start with Joel Spolsky's oft-cited article on the subject. This answer may be helpful, too.
Try calling flush()
on the output stream prior to the closeEntry()
call.
精彩评论