Skip to content

Instantly share code, notes, and snippets.

@gledsoncruz
Created March 10, 2021 17:49
Show Gist options
  • Select an option

  • Save gledsoncruz/f3dc50fb56d70776257cb6d968daacc8 to your computer and use it in GitHub Desktop.

Select an option

Save gledsoncruz/f3dc50fb56d70776257cb6d968daacc8 to your computer and use it in GitHub Desktop.
ReadZipFile.java
package com.gc.fiscaliza;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component
public class Disco {
public void salvarArquivo(MultipartFile arquivo) {
TreeMap<String, byte[]> map = new TreeMap<String, byte[]>();
try {
ZipInputStream zis = new ZipInputStream(arquivo.getInputStream());
byte buf[] = new byte[100 * 1024];
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
int read = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((read = zis.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, read);
}
baos.flush();
if (!entry.isDirectory()) {
map.put(entry.getName(), baos.toByteArray());
}
// map.put(entry.getName(), baos.toByteArray());
baos.close();
}
zis.close();
byte b[] = map.firstEntry().getValue();
BufferedReader bfReader = null;
ByteArrayInputStream is = new ByteArrayInputStream(b);
bfReader = new BufferedReader(new InputStreamReader(is));
String temp = null;
List<String> tokens = new ArrayList<>();
// String currLine = "";
StringTokenizer tokenizer;
while((temp = bfReader.readLine()) != null){
//temp.getBytes(StandardCharsets.UTF_8);
tokenizer = new StringTokenizer( temp , ";" );
while (tokenizer.hasMoreElements()) {
tokens.add(tokenizer.nextToken());
}
System.out.println(new String(temp.getBytes(Charset.forName("LATIN1"))));
}
// System.out.println(tokens);
// for(int x = 0; x < b.length; x++) {
// // printing the characters
// System.out.print((char)b[x]);
// }
// System.out.println(map.keySet().size());
// System.out.println(map.firstEntry().getValue());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// for (byte letra : map.get("PB1.csv")){
// System.out.print((char)letra);
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment