| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import 'dart:io';
- import 'package:archive/archive.dart';
- import 'package:jiffy/jiffy.dart';
- // import 'package:tp5/core/utils.dart';
- class CsvData {
- static Future<String> extractTextFileFromZip(
- String zipPath, String textFileName) async {
- // Read the Zip file from disk.
- try {
- final bytes = File(zipPath).readAsBytesSync();
- // Decode the Zip file
- final archive = ZipDecoder().decodeBytes(bytes);
- // Look for the text file in the archive
- for (final file in archive) {
- if (file.name == textFileName) {
- // Found the text file
- if (file.isFile) {
- final data = file.content as List<int>;
- String content = String.fromCharCodes(data);
- return content;
- }
- }
- }
- } finally {}
- return "";
- }
- static Jiffy? getLastModified(String zipPath, String textFileName) {
- final bytes = File(zipPath).readAsBytesSync();
- final archive = ZipDecoder().decodeBytes(bytes);
- for (final file in archive) {
- if (file.name == textFileName) {
- if (file.isFile) {
- return Jiffy.parseFromDateTime(file.lastModDateTime);
- }
- }
- }
- return null;
- }
- static List<List<dynamic>> csv2list(String text,
- {bool nulling = true, bool quotes = true, bool trim = true}) {
- List<List<dynamic>> out = [];
- final lines = text.split("\n");
- for (var line in lines) {
- final cols = line.split(",").map((String? e) {
- if (e != null && quotes) {
- e = e.replaceAll('"', "");
- }
- if (e != null && trim) {
- e = e.trim();
- }
- if (nulling) {
- e = (e == "") ? null : e;
- }
- return e;
- }).toList();
- // print("${out.length}: $cols");
- if (cols.isNotEmpty && cols.length > 1) out.add(cols);
- }
- return out;
- }
- // Usage
- void main() {
- extractTextFileFromZip('path/to/your/zipfile.zip', 'your_text_file.txt');
- }
- }
|