Unable to compress a CSV file Java - java

I'm trying to convert an existing CSV file to a gzip file.
I verified the CSV looks good. Once I run this code, I get a "failed to expand" error and tried an online decompression tool that also failed, so it seems the output zip is corrupt.
public void compressGzip(String input, String dest) throws IOException {
Path pathSource = Paths.get(input);
Path destSource = Paths.get(dest);
try (GZIPOutputStream gos = new GZIPOutputStream(
new FileOutputStream(destSource.toFile()));
FileInputStream fis = new FileInputStream(pathSource.toFile())) {
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
gos.write(buffer, 0, len);
}
}
}
Anything I could be missing here?

Related

android copy mp3 file success but duration not change

i try to copy mp3 file in external storage and limit duration of new file. it work but when it started, although i run to limit time, it still display end time like old file.
private void copy(File in, File out) throws IOException {
FileInputStream is = null;
FileOutputStream os = null;
try {
is = new FileInputStream(in);
os = new FileOutputStream(out);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0 && out.length()<175642) {
os.write(buffer, 0, length);
}
} finally {
assert is != null;
is.close();
assert os != null;
os.close();
}
}
how can i fix it. i really expect your help. have a nice day,everyone!
You simply cut of the rest of the file. This leads to a kind of corrupted file because the data in the header-block (e.g. the length) is no longer correct.
For some information about the mp3 file format:
https://en.wikipedia.org/wiki/MP3
I highly recommend to adjust the header information after shortening the file.

Rebuilding .zip file from base64 encoded .zip file clob

We've recently implemented functionality to load ".zip" files from sFTP. However before we've implemented the functionality there was already couple of ".zip" files in there which were treated as ordinary "*.xml" files and downloaded. Now I have a task to restore those downloaded files to zip format.
ZIP files were treated as xml and in doing so they were downloaded in following manner,
CLOB file = downloadFile(channelSFTP.get(fileName), oConn);
:
private static CLOB downloadFile(InputStream is, OracleConnection oConn) throws Exception {
CLOB clob = CLOB.createTemporary(oConn, true, CLOB.DURATION_SESSION);
Writer writer = clob.setCharacterStream(1);
ByteArrayOutputStream result = new ByteArrayOutputStream();
int length = -1;
byte[] buffer = new byte[1024];
while ((length = is.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
result.flush();
writer.write(result.toString("UTF-8"));
writer.flush();
return clob;
}
Then the CLOB is returned from java stored procedure to oracle procedure, and ENCODED in BASE64.
What I've tried to restore files:
public static void decodeZip(String path, String resPath) throws FileNotFoundException, IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new BASE64Decoder().decodeBuffer(new FileInputStream(path), bos);
ZipInputStream zis = new ZipInputStream((InputStream) new ByteArrayInputStream(bos.toByteArray()));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
System.out.println(entry.getName());
}
}
Where "path" is the path of the BASE64 encoded file. However I'm getting the following error:
java.util.zip.ZipException: invalid stored block lengths
on "zis.getNextEntry()". I think it should be possible to achieve this, but i can't seem to figure out, what step I'am missing.

making zip process more optimized one

I have developed a method to zip a file that will take file path and filename as a parameter and will zip a file as shown below could you please advise how can I modify this method to be more efficient and more fast as I am a big fan of optimization..
public File generateZipForAFile(String folderPath, String reportFileName)
throws FileNotFoundException, IOException {
File inputFile = new File(folderPath + reportFileName);
FileInputStream in = new FileInputStream(inputFile);
File outputZipFile = new File(folderPath, reportFileName + ".zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputZipFile));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(reportFileName ));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
out.close();
in.close();
return outputZipFile;
}
There is not much you can do in your code. Furthermore, most of the time is spent while actually doing the zip.
So even if you reduce the time spent in your part to 0 the overall gain will be very small.

Whats wrong with this zip method?

I have a method which zips up 5 files. It produces a zip file without error, but I cannot open it to examine the contents. I tried emailing it and gmail said it cannot send corrupt files. Trying to open with WinRAR in Windows results in an error stating:
The archive is either in unknown format or damaged
This is the method:
private void zipTestFiles() throws FileNotFoundException, IOException
{
File[] filenames = fileDir.listFiles(fileNameFilter(Constants.PAGE_MON_FILENAME_FILTER));
byte[] buf = new byte[1024];
String outFilename = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + Constants.PAGEMONITOR_ZIP;
DeflaterOutputStream out = new DeflaterOutputStream(new BufferedOutputStream(new FileOutputStream(outFilename)));
for (int i=0; i<filenames.length; i++)
{
FileInputStream in = new FileInputStream(filenames[i]);
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
}
out.close();
}
You should use ZipOutputStream instead of DeflaterOutputStream. And do not forget to create entries. Read javadoc of ZipOutputStream before writing the implementation.
Try with ZipOutputStream which already exists in Java. DeflaterOutputStream only uses DEFLATE method to compress but doesn't put ZIP headers automatically.

Java Compress Large File

I'm working on an app that works with some very large files each are around 180mb and there are 3 of them. I would like to add an option to my app to back these files up by compressing them in a zip or a tar or something. What is the best option would be to compress them down as much as possible in Java? Tar? Zip? Gzip?
You can do this programmatically using Apache Compress.
Alright went with zip here is the method i used. I found it online and modded it to junk the path and then just raised the buffer a little got about 450mbs of data down to 100mbs so not to bad :) thanks for the help
public void zipper(String[] filenames, String zipfile){
byte[] buf = new byte[2048];
try {
String outFilename = zipfile;
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);
File file = new File(filenames[i]);
out.putNextEntry(new ZipEntry(file.getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
}
}
Plus 1 to both of you :)

Categories